Dynamically creating matrix names in MATLAB with eval

  • Context: MATLAB 
  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
mathia
Messages
15
Reaction score
0
Hi,
I have a sample date which is a 1000*8 matrix. i want to extract some information and assign them from y data to a some matrices, i want to assign the name of those new matrices automatically, how can i do that? i used the following codes but it gave me an error!

data=load('case1.dat');
for i=1:20;
for j=1:length(data)
if data(j,1)==i;
eval['s' num2str(i)(j,:)]=data(j,:);
end
end
end

Thank you very much.

Mathias
 
Physics news on Phys.org
By sure if you can use eval like that, but you could just use sprintf in a strict instead.

Whats the error?
 
That should b struct not strict (thank you autocorrect)
 
Dera Pythagorean,
Thank you.

The error is:

Error: File: readr.m Line: 5 Column: 5
Unbalanced or unexpected parenthesis or bracket.

How can i use sprintf in a struct to do that?

i want that program make 20 matrices named s1,s2,...s20. then it check the data if data(j,1) is equal to i, then put it in the marix with the index i.

can anyone help me?
 
Last edited:
here's an example:

names = {'fred' 'sam' 'al'};
for ind = 1:length(names)
s.(names{ind}) = stuff;
end

or you can just:

s.sprintf() = stuff

check out the help file on sprintf and make sure you know how to use it first.