Questions about data types and tables
2 ビュー (過去 30 日間)
古いコメントを表示
Data types is not strong side in Matlab, so i'm hoping that someone can help me. See comments in code
%% I'm storing data types of different lenghts as a table. Is a table a good idea?
X1=[9 6 9;3 2 7];
X2=[0 2;4 0];
X3=[3 1 2; 8 9 7];
X=table(X1,X2,X3)
%% The code bellow finds the longest entity in the table and writes out any entities shorter,
% X2 in this case.
len=table2array(varfun(@(x) size(x,2),X));
lmax=max(len);
idrest=find(len<lmax);
X(:,idrest)
%% Now i wish to "fill-out" the shorter enteties by adding 'Nan' as seen bellow.
% I now wish to store this new strucutre in the place of the original. This
% is the problem that I can't solve.
for i=idrest(1):idrest(end)
temp=table2array(X(:,i));
temp(:,end+1:lmax)=nan
X.X(:,i)=temp %% Here's the issue
end
0 件のコメント
採用された回答
Dave B
2021 年 11 月 15 日
編集済み: Dave B
2021 年 11 月 15 日
In your loop, X(:,i) is the contents of the table variable, and you're using it with dot indicating it as the name of the variable.
I believe this is the code you're looking for (I added another variable to make it a slightly more robust test)
X1=[9 6 9;3 2 7];
X2=[0 2;4 0];
X3=[3 1 2; 8 9 7];
X4=[0;2];
X=table(X1,X2,X3,X4)
len=table2array(varfun(@(x) size(x,2),X)); % or varfun(@(x)size(x,2),X,'OutputFormat','uniform')
lmax=max(len);
idrest=find(len<lmax);
for i = idrest
temp=X.(i);
temp(:,end+1:lmax)=nan;
X.(i)=temp;
end
X
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で MATLAB Report Generator についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!