How to compile two different array type into one variable

1 回表示 (過去 30 日間)
Morin De Jesus
Morin De Jesus 2024 年 5 月 5 日
回答済み: Suman 2024 年 6 月 17 日
rootdir = 'C:\Users\acer\Desktop\abc';
filelist = dir(fullfile(rootdir,'**\*.xlsx'));
X = []; Y = [];
for ii = 1:length(filelist)
filelist(ii).data = importdata(fullfile(filelist(ii).folder,filelist(ii).name));
filelist(ii).data = (filelist(ii).data(:,:));
end
% combine all the data, one after the other, vertically:
all_data = vertcat(filelist.data); %this is where the error lies, since the data file were struct and cell
% How to convert all struct to cell assuming I have multiple struct file.
  1 件のコメント
Stephen23
Stephen23 2024 年 5 月 5 日
編集済み: Stephen23 2024 年 5 月 5 日
"How to compile two different array type into one variable"
What makes you think that the data can be meaningfully combined? It might be simple to combine, or it might be very complex (consisting of exception handling and special cases). We don't know.
Note that you should replace awful IMPORTDATA with a much better file-data importing function, e.g. READTABLE, READCELL, READMATRIX, or the like. Avoid IMPORTDATA.

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

回答 (1 件)

Suman
Suman 2024 年 6 月 17 日
Hi Morin,
You need to first convert the struct to cell array to be able to concatenate them. You can use the 'struct2cell' function to achieve that. https://in.mathworks.com/help/matlab/ref/struct2cell.html
Also note that, in order to concatenate cell arrays, they must be of equal dimensions. In your case, the cell arrays in the data column are of unequal dimensions, so you must either pad the cell array with zeros or NaN to make the dimensions equal before you can concatenate them. Please refer to these MATLAB Answers to see the examples,

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by