Extract Structure element dynamically

I have structure whose elements name change dynamically with respect to result file name.
I need an example code which extract structure elements from structure joinedtimetable 'joinedtimetable.Properties.VariableNames'
I need them for plotting the data like
size = numel(joinedtimetable.Properties.VariableNames)
for i = 1:numel(joinedtimetable.Properties.VariableNames)
VariableNames = ExtractName(joinedtimetable.Properties.VariableNames(i);
end
subplot(size,y,i);plot(joinedtimetable.VariableNames(1),joinedtimetable.VariableNames(2));

 Respuesta aceptada

Turlough Hughes
Turlough Hughes el 1 de Nov. de 2019
I would just convert it to a cell array which is easier to index through as you require
c=table2cell(timetable2table(joinedtimetable));
And then something like:
figure, hold on
for ii=1:size(c,2)
plot([c{:,ii}])
end
legend(joinedtimetable.Properties.VariableNames,'Interpreter','none')
How do you want to represent the non numerical data in your plots though??

6 comentarios

Life is Wonderful
Life is Wonderful el 1 de Nov. de 2019
Editada: Life is Wonderful el 1 de Nov. de 2019
I don't see the structure elements instead i get full cell arrays. Perhapes I can't use your code for analysis.
How do you want to represent the non numerical data in your plots though??
I use following code to plot text data,
endidx = size(joinedtimetable.WeirdDuration,1);
col = 'brckygmb';
subplot(1,1,1);
plot(joinedtimetable.WeirdDuration(1114-10:1114),(1114-10:1114),col(mod(endidx,7)+1)); grid on;title('servo-v4-uart');xlabel('TestTime[sec]');ylabel('Message')
text(joinedtimetable.WeirdDuration(1114-10:1114),(1114-10:1114),joinedtimetable.Message_servo_v4_uart1_servov4(1114-10:1114));
Do you have any better suggestion for plotting text data ?
I think this should answer your original question
VariableNames=joinedtimetable.Properties.VariableNames
ii=5
Your code then is modified as follows:
endidx = size(joinedtimetable.WeirdDuration,1);
col = 'brckygmb';
subplot(1,1,1);
plot(joinedtimetable.WeirdDuration(1114-10:1114),(1114-10:1114),col(mod(endidx,7)+1)); grid on;title('servo-v4-uart');xlabel('TestTime[sec]');ylabel('Message')
text(joinedtimetable.WeirdDuration(1114-10:1114),(1114-10:1114),joinedtimetable.(VariableNames{1,ii})(1114-10:1114),'Interpreter','none');
I didn't understand!!
Can you please write full snippet from data load point onwards.
For me for didn't go well. I get error. Can you please give me a working code. Thanks
>> load('matfile.mat');
>> c=table2cell(timetable2table(joinedtimetable));
>> figure, hold on
for ii=1:size(c,2)
plot([c{:,ii}])
end
Error using plot
Invalid first data argument.
This is the first option I was suggesting (updated now that I know what you are trying to plot)
load('matfile.mat')
VariableNames=joinedtimetable.Properties.VariableNames
c=table2cell(timetable2table(joinedtimetable));
endidx = size(joinedtimetable.WeirdDuration,1);
col = 'brckygmb';
figure, hold on
range=(1114-10):1114;
ii=6;
plot([c{range,1}],range,col(mod(endidx,7)+1))
grid on, title(VariableNames(1,ii-1)), xlabel('TestTime[sec]'), ylabel('Message')
text([c{range,1}],range,c(range,ii),'Interpreter','none');
Second option.
load('matfile.mat')
VariableNames=joinedtimetable.Properties.VariableNames
endidx = size(joinedtimetable.WeirdDuration,1);
col = 'brckygmb';
figure, hold on
range=(1114-10):1114;
ii=5 % Duration data is not identified with a variable, so the first variable in joinedtimetable
% is in fact log_test_that1_test_that
plot(joinedtimetable.WeirdDuration(range),range,col(mod(endidx,7)+1))
grid on, title('servo-v4-uart'), xlabel('TestTime[sec]'), ylabel('Message')
text(joinedtimetable.WeirdDuration(range),range,joinedtimetable.(VariableNames{1,ii})(range),'Interpreter','none');
Life is Wonderful
Life is Wonderful el 1 de Nov. de 2019
Super thanks a ton!! Works well
Life is Wonderful
Life is Wonderful el 2 de Nov. de 2019
Can you please help me in getting the a GUI as well ?

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 1 de Nov. de 2019
%%
s.a=1:10;
s.b=rand(1,10);
EleNames=fieldnames(s);
plot(s.(EleNames{1}),s.(EleNames{2}));

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by