Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sprintf to read inside a matrix

Subject: sprintf to read inside a matrix

From: Saad

Date: 2 Dec, 2013 16:37:09

Message: 1 of 2

Dear all,

I am trying to use a loop to read into different matrices which are: results_USA, results_UK, results_JAP and results_UK. I am trying to do it with a loop using sprintf

%% here is my code

c={'USA','EUR','JAP','UK'};

for j=1:length(c)

for i=0:10

   AA=sprintf('results_%s',c{j})

 [zMin,idx]=min(AA(:,i+1:i+5),[],2);
end

end

%% my problem is that matlab tells me that there is a dimension mismatch because it does not recognize the size of AA even though I set AA equal to "results_USA", "results_UK", "results_JAP" or "results_EUR".

I would like to use the output of sprintf('results_%s',c{j}) without having to rename the matrix "results_USA" (i.e. i would like Matlab to recognize the name of the matrix it reads)

Thank you very much for your help

S

Subject: sprintf to read inside a matrix

From: someone

Date: 2 Dec, 2013 19:02:07

Message: 2 of 2

"Saad" wrote in message <l7icvl$h64$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I am trying to use a loop to read into different matrices which are: results_USA, results_UK, results_JAP and results_UK. I am trying to do it with a loop using sprintf
>
> %% here is my code
>
> c={'USA','EUR','JAP','UK'};
>
> for j=1:length(c)
>
> for i=0:10
>
> AA=sprintf('results_%s',c{j})
>
> [zMin,idx]=min(AA(:,i+1:i+5),[],2);
> end
>
> end
>
> %% my problem is that matlab tells me that there is a dimension mismatch because it does not recognize the size of AA even though I set AA equal to "results_USA", "results_UK", "results_JAP" or "results_EUR".
>
> I would like to use the output of sprintf('results_%s',c{j}) without having to rename the matrix "results_USA" (i.e. i would like Matlab to recognize the name of the matrix it reads)
>
> Thank you very much for your help
>
> S

To do what you want to do, you will need to use the EVAL staement.
But please don't do it, your code will get VERY obfuscated.
See the MATLAB FAQ:

<<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>>

for ways to do what you want.

You will be MUCH better off in the long run renaming the matricies to cell arrays.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us