MATLAB Solving MATLAB Error: Assigning Names to New Matrices

  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around extracting information from a 1000x8 matrix and assigning it to new matrices named automatically (s1, s2, ..., s20) based on a condition. The user initially attempts to use the `eval` function in MATLAB but encounters an error related to unbalanced parentheses. Suggestions include using `sprintf` to create structured variable names instead of `eval`, with examples provided on how to assign values to a structure using dynamic field names. The conversation highlights the importance of understanding the correct syntax for `sprintf` and struct usage in MATLAB to achieve the desired outcome without errors.
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.
 

Similar threads

Replies
2
Views
3K
Replies
8
Views
2K
Replies
12
Views
3K
Replies
3
Views
3K
Replies
2
Views
2K
Replies
3
Views
7K
Back
Top