Borrar filtros
Borrar filtros

convert cell of cells to individual variables

6 visualizaciones (últimos 30 días)
Anthony Sirico
Anthony Sirico el 9 de Sept. de 2023
Editada: Stephen23 el 9 de Sept. de 2023
If you take a look at the objectives photo, i have a cell that contains 32612x1 cells, each will either contain a 200x5 double, or [ ]. The objective1 photo shows what a cell with 200x5 looks like. What I want to do is is take Objective(1), a 200x5 cell, and then column 1 of that cell, and make it J1. Column 2 will be J2, etc. I want to do that for each 32612 cells. If the cell is empty, not to skip, to put a place holder there.
Ultimately what i am trying to do, is bring it over to python so i can upload J1, J2, etc, into spandas dataframes, where J1 will be one dataframe of 32612x200. I tried using jsonencode but its still not formatted correctly.
  1 comentario
Stephen23
Stephen23 el 9 de Sept. de 2023
Editada: Stephen23 el 9 de Sept. de 2023
"....and then column 1 of that cell, and make it J1. Column 2 will be J2, etc."
"Ultimately what i am trying to do, is bring it over to python so i can upload J1, J2, etc, into spandas dataframes, where J1 will be one dataframe of 32612x200."
"I tried using jsonencode but its still not formatted correctly."
Given that the string returned by JSONENCODE does not use the input variable name, your request serves no purpose other than to make your code pointlessly complex and inefficient: you could simply use indexing and get exactly the same result.
Whether JSONENCODE give you the expected output is another question entirely.
Perhaps this does what you want:
Note that you can also call Python directly from MATLAB, so probably you could just save the dataframe using... Pandas. It really is very easy (and much much better than writing slow, inefficient, complex code using dynamic variable names):

Iniciar sesión para comentar.

Respuestas (1)

Bruno Luong
Bruno Luong el 9 de Sept. de 2023
Editada: Bruno Luong el 9 de Sept. de 2023
assuming c is your (32612 x 1) cell
% Dummy data to test
c=cell(32612,1);
for i=1:size(c,1)
c{i} = rand(200,5);
end
[c{randi(end,1,1000)}] = deal([]);
[T1, T2, T3, T4, T5] = convT(c);
function [T1, T2, T3, T4, T5] = convT(c)
[c(cellfun(@isempty,c))] = deal({nan(200,5)}); % empty "holder"
A = permute(cat(3, c{:}),[3 1 2]); % 32611 x 200 x 5
s = num2cell(A,[1 2]);
s = s(:);
assert(size(s,1)==5, 'data insode c must have 5 columns')
T1 = s{1};
T2 = s{2};
T3 = s{3};
T4 = s{4};
T5 = s{5};
% for k=1:size(s,3) % 5 here
% eval([sprintf('T%d',k) ' = s{k};']); % creat T1, ... T5
% %T.(sprintf('T%d',k)) = s{k};
% end
end
  1 comentario
Stephen23
Stephen23 el 9 de Sept. de 2023
Editada: Stephen23 el 9 de Sept. de 2023
With only five hard-coded output arugments that EVAL is completely superfluous.
Five basic variable assignments would be simpler and more efficient. Or using VARARGOUT:
function varargout = convT(c)
[c(cellfun(@isempty,c))] = deal({nan(200,5)}); % empty "holder"
A = permute(cat(3, c{:}),[3,1,2]); % 32611 x 200 x 5
varargout = num2cell(A,[1,2]);
end

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by