MATLAB Answers

perform an operation on a series of varibles with different names

2 views (last 30 days)
giacomo labbri
giacomo labbri on 1 Feb 2021
Commented: giacomo labbri on 6 Feb 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 Comment
Stephen Cobeldick
Stephen Cobeldick on 6 Feb 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.

Sign in to comment.

Answers (1)

Shashank Gupta
Shashank Gupta on 4 Feb 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 Comments
giacomo labbri
giacomo labbri on 6 Feb 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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by