Matlab code but i'm having problems puting in a loop

Click For Summary
SUMMARY

The discussion centers on optimizing MATLAB code by implementing a loop to extract specific columns from multiple variable arrays. The user initially struggles with dimension mismatches and variable size inconsistencies when attempting to store results in a pre-defined array. Ultimately, the user successfully creates a working loop to extract the desired data from arrays named in a sequential manner (e.g., p1t4a, p2t4a). The function findvarible is defined to facilitate column extraction, and the user expresses satisfaction upon resolving the looping issue.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of array manipulation in MATLAB
  • Knowledge of dynamic variable naming and evaluation using eval
  • Basic understanding of function creation in MATLAB
NEXT STEPS
  • Explore MATLAB's cell arrays for handling arrays of varying sizes
  • Learn about MATLAB's struct data type for better organization of related data
  • Investigate the use of arrayfun for applying functions to arrays
  • Study best practices for using eval in MATLAB to avoid potential pitfalls
USEFUL FOR

MATLAB programmers, data analysts, and researchers who need to efficiently manipulate and extract data from multiple variable arrays in their projects.

manicwhite
Messages
6
Reaction score
0
Hi. i have seen similar questions on here but can't quite get them to apply to my problem

i have wrote some MATLAB code but I'm having problems puting in a loop

heres my code
-----------------------------------------------------------------------------------------

clear
clc
for i=1:10
name=['C:\Documents and Settings\john\My Documents\matlabfiles\t2\p',num2str(i),'t2a.asc'];
load(name);

end
clear name

track1=findvarible(p1t2a,31);
track2=findvarible(p2t2a,31);
track3=findvarible(p3t2a,31);
track4=findvarible(p4t2a,31);
track5=findvarible(p5t2a,31);
track6=findvarible(p6t2a,31);
track7=findvarible(p7t2a,31);
track8=findvarible(p8t2a,31);
track9=findvarible(p9t2a,31);
track10=findvarible(p10t2a,31);
--------------------------------------------------------------------------------
i would like to put the last block into a loop

the files pit2a are arays of varying size (x,66). i want to take one column of interest out
i have tried puting these values into an aray 'track' like this.

---------------------------------------------------------------------------
% track=zeros(85,66)
% for i=1:10
% track(:,i)=findvarible(eval(['p',int2str(i),'t4a']),31);
% end
--------------------------------------------------------------------------
this caused me problems because the variables were of different sizes so i tried pre defining the aray to the biggest varible but that didnt work


the function findvarible is(see below). its in there for an adition i intend to add later
-------------------------------------------------------------------
function [ varible ] = findvarible( filename,var )
varible=filename(:,var);
end
--------------------------------------------------------------------

i have tried using a lop to do it like the top bit automaticaly but i just can't get it to work.


so anyways. any help that puts me in the right direction to geting that top block into a loop would be helpfull

hope that wasnt to wishywashy
 
Physics news on Phys.org


tried this

%track(:,1)=p1t4a(:,31);% this line checks it can work works

for i=1:10
name=['p',int2str(i),'t4a']
track(:,i)=name(:,31)
end
got this error

? Index exceeds matrix dimensions.

Error in ==> test4compare at 14
track(:,i)=name(:,31)
 


i think i already mentioned something similar to this

for i=1:10
name=['p',int2str(i),'t4a']
track(:,i)=eval(['p',int2str(i),'t4a(:,31)'])
end

the first one runs then i get an error saying
? Subscripted assignment dimension mismatch.

Error in ==> test4compare at 14
track(:,i)=eval(['p',int2str(i),'t4a(:,31)'])
 


for i=1:10
name=['p',int2str(i),'t4a']
eval(['track', int2str(i) '=p',int2str(i),'t4a(:,31)'])
end

ok i have a working loop. i am happy. if you have a better solution to the problem please still post
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
704
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K