Questions about data types and tables
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dave B
on 15 Nov 2021
Edited: Dave B
on 15 Nov 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!