How to assign x number of variables in a loop...

1 visualización (últimos 30 días)
Bryan
Bryan el 7 de Jun. de 2013
I am trying to write a function for a program that extracts information from .dat files and puts it into excel. Each .dat file has multiple sets of information that are all different sized matrices.
The problem is that each file can have varying number of these tests. I want to be able to input the number of tests that are in a series of .dat files and have the loop compensate for it. There are hundreds of .dat files for each setup that might have 8 or 12 or more tests each.
Here’s what I have now:
R1=zeros(m(1),n(1)); legend1=cell(1,n(1));
for k = 1:m(1)
for i = 1:n(1)
r1(k,i)=str2double(C(k+i-1+(k-1)*(n(1)-1)+s(1)));
legend1(i)=C(s(1)-n(1)+i);
end
end
r2=zeros(m(2),n(2)); legend2=cell(1,n(2));
for k = 1:m(2)
for i = 1:n(2)
r2(k,i)=str2double(C(k+i-1+(k-1)*(n(2)-1)+s(2)));
legend2(i)=(C(s(2)-n(2)+i));
end
end
R12=zeros(...
etc...
Here’s what I’d like to do:
user_input=12; % 12 tests for this run
for j=1:user_input
C=sprintf('A%d', j);
L=sprintf('Legend%d',j);
end
for j = 1:length(C)
for k = 1:m(1)
for i = 1:n(1)
C(j)(k,i)=str2double(C(k+i-1+(k-1)*(n(j)-1)+s(j)));
L(j)(i)=C(s(1)-n(j)+i);
end
end
end
Obviously that doesn’t work, but is there another way?

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Jun. de 2013
  2 comentarios
Bryan
Bryan el 8 de Jun. de 2013
So i looked at the page (I saw it linked on another question) but it doesn't seem to answer my question or my desire.
If i can do a cell array or a structure, I'd like to be able to put each part of the matrix in one at a time.
if A=[{[1 2; 3 4]},{[5 6; 7 8]}]
how do I extract ONE single number from this? A(1,1,1) gets the entire first matrix not A(1)(1,1). I was hoping to get the number "1" or "2" from the first matrix. this of course would be the opposite of generating a file, but I might get an idea how to do it.
Bryan
Bryan el 8 de Jun. de 2013
I think this ended up working...
% setup info
C=[1 2 3 4 5 6 7 8 9 10 11 12];
m=[1 1 2];
n=[3 3 3];
s=[1 2 3];
% loop
for j = 1:length(s)
for k=1:m(j)
for i = 1:n(j)
r(k,i)=C((i+k-1+(n(j)-1)*(k-1))+s(j));
end
end
D{j}=r;
end
I think I should be able to export the whole matrix to excel this way... I guess I'll check when I get back to a windows computer.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by