How to Generate Database from Cell array In Table form in MATLAB

3 visualizaciones (últimos 30 días)
Hammad Younas
Hammad Younas el 13 de Feb. de 2023
Editada: Vinayak Choyyan el 13 de Feb. de 2023
Hello. I have import Data from website. I need to convert the Cell array in Table with Text in Column and corresponding to there Values.
For Example In the following data I have 1x4 cell. The first Cell Predicted Animal is the Text of column and Cat is the Coresponding Value.
The second Cell of the first will be Text of column Maximum, Minimum and Mean Value corresponding to there Values.
Similarly the Remaining Cell have the values
The 2nd Cell in first Predicted Class Airplane is the Text of column and Fighter is the Value. The second Cell Fighter Levels, Fighter Values, Maximum, Minimum and Mean Value should be in column
I have attached the Image of output that i wanted. Can anybody Help me with that.
  2 comentarios
Stephen23
Stephen23 el 13 de Feb. de 2023
" I have import Data from website."
Most likely this task would be easier using the original URL/text source, rather than your cell array of nested cell arrays.

Iniciar sesión para comentar.

Respuestas (2)

埃博拉酱
埃博拉酱 el 13 de Feb. de 2023
cell2table

Vinayak Choyyan
Vinayak Choyyan el 13 de Feb. de 2023
Editada: Vinayak Choyyan el 13 de Feb. de 2023
Hello Hammad,
As per my understanding, you have a mat file with a variable Database. You would like to parse the data and get it into a ‘table’ in MATLAB.
Please check this code below. It loads the mat file, selects each cell from the 1x4 cells. Then these each cell is further divided into 2 cells and parsed. The results of parsing are in t1, t2, t3, t4, t5. I had manually given the headings for the table. If you wish, you can also get this from the parsed data. For example,
t1(1)+t1(2)
will give the heading PredictedAnimals.
clc;clear;
load("Database.mat");
cellRow1Column1=Database{1};
t1=cellRow1Column1{1,1};
t1=customParser(t1);
t2=cellRow1Column1{1,2};
t2=customParser(t2);
cellRow1Column2=Database{2};
t3=cellRow1Column2{1,1};
t3=customParser(t3);
t4=cellRow1Column2{1,2};
t4=customParser(t4);
cellRow1Column3=Database{3};
t5=cellRow1Column3{1,1};
t5=customParser(t5);
T=table(string(t1(3)),str2double(t2(3)),str2double(t2(6)),str2double(t2(9)),string(t3(3)),str2double(t4(3)),str2double(t4(6)),str2double(t4(7)),str2double(t4(8)),str2double(t4(14)),string(t5(3)),'VariableNames',{'PredictedAnimals','MaximumValue','MinimumValue','MeanValue','PredictedAirplane','FighterLevels','FighterValues_1','FighterValues_2','FighterValueMaximumValue','FighterValueMinimumValue','PredictedCar'})
T = 1×11 table
PredictedAnimals MaximumValue MinimumValue MeanValue PredictedAirplane FighterLevels FighterValues_1 FighterValues_2 FighterValueMaximumValue FighterValueMinimumValue PredictedCar ________________ ____________ ____________ _________ _________________ _____________ _______________ _______________ ________________________ ________________________ ____________ "Cat" 1000 1000 1000 "Fighter" 3 100 800 900 100 "Honda"
function y=customParser(x)
pat = "C" + digitsPattern(1,10);
y = split(x,[" ",":","[","]",pat]);
y=y(strlength(y)>0);
end
I hope this resolves the issue you are facing. If there are multiple rows in the variable Database, please put the above code in a loop and make appropriate changes.
If you would like to have more information on the functions used, please read the following documents.
  2 comentarios
Hammad Younas
Hammad Younas el 13 de Feb. de 2023
@Vinayak Choyyan Thank for you answer, But I want to give the heading from the cell not manually.
Can you please help me with that?
Vinayak Choyyan
Vinayak Choyyan el 13 de Feb. de 2023
Editada: Vinayak Choyyan el 13 de Feb. de 2023
Please check the below modified code to give the headings from cells and not manually.
clc;clear;
load("Database.mat");
cellRow1Column1=Database{1};
t1=cellRow1Column1{1,1};
t1=customParser(t1);
t2=cellRow1Column1{1,2};
t2=customParser(t2);
cellRow1Column2=Database{2};
t3=cellRow1Column2{1,1};
t3=customParser(t3);
t4=cellRow1Column2{1,2};
t4=customParser(t4);
cellRow1Column3=Database{3};
t5=cellRow1Column3{1,1};
t5=customParser(t5);
%T=table(string(t1(3)),str2double(t2(3)),str2double(t2(6)),str2double(t2(9)),string(t3(3)),str2double(t4(3)),str2double(t4(6)),str2double(t4(7)),str2double(t4(8)),str2double(t4(14)),string(t5(3)),'VariableNames',{'PredictedAnimals','MaximumValue','MinimumValue','MeanValue','PredictedAirplane','FighterLevels','FighterValues_1','FighterValues_2','FighterValueMaximumValue','FighterValueMinimumValue','PredictedCar'})
x{1}=convertStringsToChars(string(t1(1))+string(t1(2)));
x{2}=convertStringsToChars(string(t2(1))+string(t2(2)));
x{3}=convertStringsToChars(string(t2(4))+string(t2(5)));
x{4}=convertStringsToChars(string(t2(7))+string(t2(8)));
x{5}=convertStringsToChars(string(t3(1))+string(t3(2)));
x{6}=convertStringsToChars(string(t4(1))+string(t4(2)));
x{7}=convertStringsToChars(string(t4(4))+string(t4(5))+"_1");
x{8}=convertStringsToChars(string(t4(4))+string(t4(5))+"_2");
x{9}=convertStringsToChars(string(t4(4))+string(t4(5))+string(t4(9))+string(t4(10)));
x{10}=convertStringsToChars(string(t4(4))+string(t4(5))+string(t4(12))+string(t4(13)));
x{11}=convertStringsToChars(string(t5(1))+string(t5(2)));
T=table(string(t1(3)),str2double(t2(3)),str2double(t2(6)),str2double(t2(9)),string(t3(3)),str2double(t4(3)),str2double(t4(6)),str2double(t4(7)),str2double(t4(8)),str2double(t4(14)),string(t5(3)),'VariableNames',x)
T = 1×11 table
PredictedAnimal MaximumValue MinimumValue MeanValue PredictedAirplane FighterLevels FighterValues_1 FighterValues_2 FighterValuesMaximumValue FighterValuesMinimumValue PredictedCar _______________ ____________ ____________ _________ _________________ _____________ _______________ _______________ _________________________ _________________________ ____________ "Cat" 1000 1000 1000 "Fighter" 3 100 800 900 100 "Honda"
function y=customParser(x)
pat = "C" + digitsPattern(1,10);
y = split(x,[" ",":","[","]",pat]);
y=y(strlength(y)>0);
end

Iniciar sesión para comentar.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by