perform an operation on a series of varibles with different names

2 visualizaciones (últimos 30 días)
giacomo labbri
giacomo labbri el 1 de Feb. de 2021
Comentada: giacomo labbri el 6 de Feb. de 2021
Hi,
I would like to perform the same operation on a series of differet variables. Here are the details:
I have some different variables that have been uploaded from different mat files. Let's say:
u1=[1,3,4,6];
u2=[1,5,7,2];
v1=[4,2,3,1];
v2=[2,3,4,2];
u1 and v1 are loaded from file1.mat, u2 and v2 are loaded from file2.mat;
i would like to apply plo2car() function to the corrsponding couples of (u1,v1) and (u2,v2)
any idea?
  1 comentario
Stephen23
Stephen23 el 6 de Feb. de 2021
Editada: Stephen23 el 6 de Feb. de 2021
"I have some different variables that have been uploaded from different mat files"
and that is exactly where you need to fix this bad data design. Instead of loading directly into the workspace (bad idea) you should always load into an output variable (which is a scalar structure):
S = load(..);
The fields of that structure contain the arrays from the mat file. You can access them as you wish, even with dynamic fieldnames:
Much much better would be if every mat file used exactly the same variable names, then your code would be simple and efficient.

Iniciar sesión para comentar.

Respuestas (1)

Shashank Gupta
Shashank Gupta el 4 de Feb. de 2021
Hi,
there are multiple way of doing this, but most convenient and structured way would be make a matrix u & v where each column correspond to ui & vi. Converting them into matrix would be easy to use in the function call.
I am attaching a piece of code for your reference.
% Convert all u's and v's into a matrix.
% Considering you have the matrix formed and
% want to call the function with u1 & v1.
plo2car(u(:,1),v(:,1))
One more good thing about converting them into matrix is you can easily put in under a loop and call the function multiple times.
I hope this helps.
Cheers
  3 comentarios
Stephen23
Stephen23 el 6 de Feb. de 2021
Editada: Stephen23 el 6 de Feb. de 2021
"Creating it by heand is not possible becuase I have a big number of variables u1,u2....."
Numbering variables like that is a sign that you are doing something wrong.
Do NOT load the mat files directly into the workspace.
Load into an output variable. In the loop which you use for importing the file data use indexing to allocate the imported data to one or two arrays. The documentation shows the basic concept:
giacomo labbri
giacomo labbri el 6 de Feb. de 2021
Thanks, the problem is that I am reading the variables from .mat files (produced by another program) so when the variables are loaded they already have names. (Even if I load them into a structure). Of course I could modify the other program do have another output to import. But if there is a solution to perform an operation on a series of variables it would be much fatser for me at this point (even if it not the best way to do it)

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by