perform an operation on a series of varibles with different names
3 views (last 30 days)
Show older comments
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
Stephen23
on 6 Feb 2021
Edited: Stephen23
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.
Answers (1)
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
Stephen23
on 6 Feb 2021
Edited: Stephen23
on 6 Feb 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:
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!