Combining consecutive vectors into a matrix

조회 수: 2 (최근 30일)
Lars Bouten
Lars Bouten 2019년 10월 2일
편집: Guillaume 2019년 10월 2일
I am trying to combine 108 consecutive vectors (100x1 double) into a matrix in such a way that I will get a 100x108 matrix. The vectors are called like HH_1, HH_2, etc., so I can do it by using M=[HH_1,HH_2,...], but this is really a lot of work for 108 vectors. Therefore, I was wondering if there is a short and easy way to perform this action. After I obtain the 100x108 matrix, I want to seperate this matrix into 100 rows, how can I do this easily?
  댓글 수: 4
Guillaume
Guillaume 2019년 10월 2일
But you do you get then into matlab? Are they already saved in a mat file?
Lars Bouten
Lars Bouten 2019년 10월 2일
I imported them from a .xlsx file via the Matlab import data wizard for Excel sheets.

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

채택된 답변

the cyclist
the cyclist 2019년 10월 2일
편집: the cyclist 2019년 10월 2일
You should very much try to work backward and try to get these dynamically named variables into MATLAB in a different way. For example, if you are somehow reading them in from a database via a for loop, maybe write directly into a matrix from there. Or something better than dynamically named variables!
Please read this tutorial.
But, if you are really, truly stuck with these variable names ... yes, you can work with them.
I will probably forfeit all of my Answers street cred for even mentioning eval, but ...
HH_all = zeros(100,108);
for nh = 1:108
eval(sprintf('HH_all(:,%d) = HH_%d',nh,nh))
end
will write each of your variables into a column of the new variable HH_all.

추가 답변 (1개)

Guillaume
Guillaume 2019년 10월 2일
편집: Guillaume 2019년 10월 2일
You must have changed the options of the wizard to specifically create variables from the columns of the excel spreadsheet. Don't do that. Import the data as a table (best), or cell array and your problem will go away.
Or better, don't use the import wizard. Use readtable, readmatrix (if on a version new enough) or xlsread.
Obtaining your M matrix may be as simple as :
M = readmatrix(yourexcelfile);
If not, an example file would help us understand better.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by