How to Generate Database from Cell array In Table form in MATLAB
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 13 Feb. 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.
Antworten (2)
Vinayak Choyyan
am 13 Feb. 2023
Bearbeitet: Vinayak Choyyan
am 13 Feb. 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'})
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 Kommentare
Vinayak Choyyan
am 13 Feb. 2023
Bearbeitet: Vinayak Choyyan
am 13 Feb. 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)
function y=customParser(x)
pat = "C" + digitsPattern(1,10);
y = split(x,[" ",":","[","]",pat]);
y=y(strlength(y)>0);
end
Siehe auch
Kategorien
Mehr zu Database Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!