Correctly storing data in cell arrays
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi there. I am trying to correltly store my data in cell arrays but I am failing.
This is my code:
txtf = 'kimetsu.txt';
exfile = 'corps_members.xlsx';
Members = readcell(txtf);
sheets = sheetnames(exfile);
g = cellstr(sheets);
urokodaki = cell(numel(g),2);
for i = 1:numel(g)
urokodaki(i,:) = [g(i), {readcell(exfile,'Sheet',i,'NumHeaderLines',1)}];
end
mant = urokodaki;
df =[];
for p = 1:size(urokodaki, 1)
[ism,idx] = ismember(urokodaki{p,2}(:,4),Members(:,1));
mant{p,2}(ism,4) = Members(idx(ism),2);
mant{p,2}(~ism,4) = {0};
col1 = mant{p,2}(:,1);
col2 = mant{p,2}(:,2);
% disp([col1 col2]) %gives the right output
df = [df; col1 col2]; % this attaches the bounds of the data and I want spacing between the data
end
disp(df)
This is what I want to get first:
{[1]} {[12]}
{[2]} {[12]}
{[3]} {[10]}
{[4]} {[ 4]}
{[5]} {[12]}
{[1]} {[12]}
{[2]} {[12]}
{[3]} {[10]}
{[4]} {[ 4]}
but this is what I am getting: (the problem with this is that the data is "touching" on the upper and lower bounds). I want there to be spacing between 1-5 and 1-4 but I want this done with a generic code that does not assume that there will always be 1-5 and 1-4. The numbers can change and be something like 1-8, 1-11, 1-13 etc.
{[1]} {[12]}
{[2]} {[12]}
{[3]} {[10]}
{[4]} {[ 4]}
{[5]} {[12]}
{[1]} {[12]}
{[2]} {[12]}
{[3]} {[10]}
{[4]} {[ 4]}
And then finally store the data in a cell array of this sort:
2×2 cell array
{'Sheet1'} {5×2 cell}
{'Sheet2'} {4×2 cell}
7 comentarios
Stephen23
el 27 de Mayo de 2022
Editada: Stephen23
el 27 de Mayo de 2022
Why are you using READCELL when later you just convert this to a numeric matrix?:
Use READMATRIX and avoid messing around with nested cell arrays.
Simpler data design would be to store that data in one table, adding one column/variable to indicate the origin, and make use of the tools that work well with tables, e.g.: https://www.mathworks.com/help/matlab/ref/splitapply.html#bux1l61
dpb
el 27 de Mayo de 2022
I agree w/ @Stephen23 in that even though what showed you will do what you say, this is almost certainly the hard way to go about whatever it is that you're after.
I'd start with something like
>> readtable('corps_members.xlsx','Sheet',1)
ans =
5×4 table
CorpsMembers Rank Style Names
____________ _____ _____ ___________
1.00 12.00 1.00 {'Tanjiro'}
2.00 12.00 1.00 {'Guyi' }
3.00 10.00 1.00 {'Akaza' }
4.00 4.00 2.00 {'Rengoku'}
5.00 12.00 2.00 {'Shenobu'}
>> readtable('corps_members.xlsx','Sheet',2)
ans =
4×4 table
CorpsMembers Rank Style Names
____________ _____ _____ ___________
1.00 12.00 1.00 {'Tanjiro'}
2.00 12.00 1.00 {'Guyi' }
3.00 10.00 1.00 {'Rengoku'}
4.00 4.00 2.00 {'Zenitsu'}
>>
Looks like simply appending the sheets and then using unique w/ rows option would give you the database of members; it's not at all clear then what the next step would be with the additional data in the other file -- but one could then add that data to the above for the members in the list; either with missing values for those not or then reduce the table to only those in the other list.
Respuestas (1)
Prahlad Gowtham Katte
el 8 de Jun. de 2022
Hello!
As per my understanding of the query , you wish to have a gap between printing data of 2 different cells. You can use “empty” function to do the same. Following code snippet will print the data as required.
txtf = 'kimetsu.txt';
exfile = 'corps_members.xlsx';
Members = readcell(txtf);
sheets = sheetnames(exfile);
g = cellstr(sheets);
urokodaki = cell(numel(g),2);
for i = 1:numel(g)
urokodaki(i,:) = [g(i), {readcell(exfile,'Sheet',i,'NumHeaderLines',1)}];
end
empty=repmat({blanks(1)},1,2);% this creates an empty cell of 1x2 which can be inserted after each iteration of for loop.
mant = urokodaki;
df =[];
for p = 1:size(urokodaki, 1)
[ism,idx] = ismember(urokodaki{p,2}(:,4),Members(:,1));
mant{p,2}(ism,4) = Members(idx(ism),2);
mant{p,2}(~ism,4) = {0};
col1 = mant{p,2}(:,1);
col2 = mant{p,2}(:,2);
df = [df; col1 col2;empty];
end
disp(df(1:end-1,:))
For more information on the “blanks” and “empty” functions please refer to the following links
Hope it helps!
0 comentarios
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!