# perform an operation on a series of varibles with different names

조회 수: 1(최근 30일)
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 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):
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 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표시숨기기 이전 댓글 수: 2
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)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by