Solving MATLAB Error: Assigning Names to New Matrices

  • Context: MATLAB 
  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming issue related to dynamically creating and naming matrices based on data extracted from a larger matrix. Participants are exploring methods to automate the naming of these matrices and troubleshoot errors encountered in the process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant, Mathias, describes an attempt to create 20 matrices named s1, s2, ..., s20 using a loop and the eval function, but encounters an error related to unbalanced parentheses.
  • Another participant suggests that using eval in the proposed manner may not be appropriate and recommends using sprintf instead, although the exact implementation is not clarified.
  • A later reply corrects a typo in the previous message, indicating that the term should be "struct" instead of "strict." This suggests a focus on using structures in MATLAB for dynamic naming.
  • Mathias seeks clarification on how to implement sprintf within a struct to achieve the desired outcome of creating named matrices based on conditions in the data.
  • One participant provides an example of using a cell array of names to assign values to a struct, indicating a potential solution to the naming issue.

Areas of Agreement / Disagreement

Participants express differing views on the use of eval for dynamic naming, with some suggesting alternative methods. The discussion remains unresolved regarding the best approach to achieve the desired functionality without errors.

Contextual Notes

Participants have not fully resolved the specific implementation details of using sprintf or struct for dynamic matrix naming, and there are indications of potential misunderstandings regarding MATLAB syntax.

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 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K