perform an operation on a series of varibles with different names

3 ビュー (過去 30 日間)
giacomo labbri
giacomo labbri 2021 年 2 月 1 日
コメント済み: giacomo labbri 2021 年 2 月 6 日
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 件のコメント
Stephen23
Stephen23 2021 年 2 月 6 日
編集済み: Stephen23 2021 年 2 月 6 日
"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.

サインインしてコメントする。

回答 (1 件)

Shashank Gupta
Shashank Gupta 2021 年 2 月 4 日
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 件のコメント
Stephen23
Stephen23 2021 年 2 月 6 日
編集済み: Stephen23 2021 年 2 月 6 日
"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 2021 年 2 月 6 日
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)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by