Correctly storing data in cell arrays

2 views (last 30 days)
Andromeda
Andromeda on 26 May 2022
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 Comments
dpb
dpb on 27 May 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.

Sign in to comment.

Answers (1)

Prahlad Gowtham Katte
Prahlad Gowtham Katte on 8 Jun 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!

Community Treasure Hunt

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

Start Hunting!