Matrix Maipulation and adding from variables in the workspace

조회 수: 2 (최근 30일)
arun Dhillon
arun Dhillon 2019년 5월 9일
댓글: arun Dhillon 2019년 5월 13일
I am saving a file(penmax), which has n no. of variables. In each variable, the size is always 1*5 (1 row, 5 cols.); the names of variables are liuke pick1, pick2,pick3...but the the no of variables inside the penmax file changes each time i run the script.
What I want to do is, to save all the variables inside of the penmax file into an m*5 matrix(newmatrix). I cannot use [] to concatenate each variable with the other one as I am not sure for how many variables penmax may have in total; but I am sure for each of the variables will have 1 row and 5 cols. Please edify me for how can I put all the variables of penmax into an n*5 matrix(newmatrix).
The file format is penmax.mat
  댓글 수: 4
Stephen23
Stephen23 2019년 5월 10일
편집: Stephen23 2019년 5월 10일
"Essentially lets say, you simply variabes in the workspace from a1,a2,...aN. "
I would never have numbered variables like that in my workspace, so that question is moot.
Having numbered variables is a sign that code is very badly designed. Usually what happens with numbered variables is that beginners try to access them dynamically by name. Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Avoiding such badly design code is very easy, usually using one array and some indexing is all that is required. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
I doubt that you sat and wrote all of those variable names out by hand, so wherever you imported/generated those arrays, instead simply allocate them into one (preallocated) array (which could be numeric, cell, structure, table, etc.):
arun Dhillon
arun Dhillon 2019년 5월 13일
You are right,
The code is not designed very well, and I am trying to rectify the structure; but thanks a lot for your advise.

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

답변 (1개)

Stephen23
Stephen23 2019년 5월 10일
Load the file into a variable (which is a structure) and access its fieldnames:
S = load(...)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by