Is it possible to convert a cell array into individual variables on workspace?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a 4x2 cell array of names and their values. I am trying to save them as a separate variable on workspace so that i can use them in my code. Is it possible to convert them into individual arrays?
Example: My cell array is
>> y(:,1)={'force';'mass';'distance';'gravity';'Unit'};
>> y(:,2)={'10';'0.5';'5';'10','N'};
% Desired Output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/544272/image.jpeg)
0 comentarios
Respuestas (1)
Are Mjaavatten
el 9 de Mzo. de 2021
A better idea is to create a struct:
for i = 1:size(y,1)
s.(y{i,1}) = y{i,2};
end
2 comentarios
Stephen23
el 9 de Mzo. de 2021
y = {'force';'mass';'distance';'gravity';'Unit'};
y(:,2) = {'10';'0.5';'5';'10';'N'};
either
S = cell2struct(y(:,2),y(:,1))
or
y = y.';
S = struct(y{:})
Steven Lord
el 9 de Mzo. de 2021
y = {'force';'mass';'distance';'gravity';'Unit'};
y(:,2) = {10;0.5;5;10;'N'};
T =cell2table(y(:, 2).', 'VariableNames', y(:, 1))
T.distance
T{1, 'gravity'}
You could also set the RowNames of the table so you can do things like:
T.Properties.RowNames = {'trial1'}
T{'trial1', 'force'}
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!