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

In summary, the conversation is about a person having trouble putting a block of code into a loop in their MATLAB program. They have tried using different methods, such as creating an array and using the function "findvarible," but have encountered errors due to the varying sizes of the variables. After some trial and error, they finally find a working solution. They ask for any suggestions on how to improve their code.
  • #1
manicwhite
6
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
  • #2


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)
 
  • #3


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)'])
 
  • #4


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
 
  • #5


Hi there,

It seems like you are having trouble putting your code into a loop because the variables you are trying to loop through are of different sizes. One solution could be to predefine the array 'track' to be the size of the largest variable, and then use a conditional statement within the loop to only add the column of interest if it exists in the current variable.

For example:

track = zeros(max_size, 66); % predefine track to be the size of the largest variable

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

% use a conditional statement to only add the column if it exists in the current variable
if size(pit2a, 2) >= 31
track(:,i) = findvarible(pit2a, 31);
end
end

This way, the loop will only add the column to 'track' if it exists in the current variable, and the rest of the columns will remain as zeros.

I hope this helps and puts you on the right track. Let me know if you have any further questions or if this solution doesn't work for you. Good luck with your code!
 

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

How do I create a loop in Matlab code?

To create a loop in Matlab code, you can use the "for" or "while" keywords followed by the desired number of iterations or a logical condition respectively. For example, a for loop that runs 10 times would look like this:
for i = 1:10
% loop body
end

How can I troubleshoot my loop in Matlab code?

To troubleshoot a loop in Matlab code, you can use the "disp" function to display the value of any variables or conditions within the loop. This can help identify where the loop may be going wrong. You can also use the "pause" function to stop the loop at certain points and check the values of variables. Additionally, using the "debug" mode in Matlab can also help you step through the loop and identify any errors.

What is the difference between a for loop and a while loop in Matlab?

A for loop is used to repeat a set of commands for a specified number of iterations, while a while loop is used to repeat a set of commands until a certain condition is met. This means that in a for loop, the number of iterations is predetermined, whereas in a while loop, the loop will continue until the condition is no longer met.

How can I optimize my loop in Matlab code?

To optimize a loop in Matlab code, you can try to minimize the number of calculations or operations within the loop, as these can slow down the loop. You can also preallocate arrays or variables before the loop starts, as this can improve the speed of the loop. Additionally, using built-in functions or vectorization can also help optimize loops in Matlab.

Can I nest loops in Matlab code?

Yes, you can nest loops in Matlab code. This means that you can have a loop within another loop. However, it is important to be mindful of how many iterations this may create, as it can significantly slow down the code. It is also important to ensure that the nested loops do not create an infinite loop, as this can cause the code to crash.

Similar threads

Replies
1
Views
1K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
1
Views
6K
Replies
4
Views
2K
Back
Top