0

I'm creating a table containing the results of my experiments. I'm having troubles with variables definition in the for loop. I had no problems when I did the exact same thing without a loop, so I think I need at least a hint.

So far this is what I've done, after preallocation:

for t = 1:N
Bands(t) = {'Band %d',t};
Diameter(t) = dn;
Bandwidth(t) = barray(t);
n1(t) = R.m(t);
n2(t) = R2.m2(t);
P1(t) = round(p(t),2);
P2(t) = round(p2(t),2);
Damage(t) = round(dad(t),2);
end
Bands(t+1) = {'Total'};
Diameter(t+1) = {'-'};
Bandwidth(t+1) = sum(barray);
n1(t+1) = sum(R.m);
n2(t+1) = sum(R2.m2);
P1(t+1) = round(ptot,2);
P2(t+1) = round(ptot2,2);
Damage(t+1) = round(damage,2);

Bands = Bands';
Diameter = Diameter';
Bandwidth = Bandwidth';
n1 = n1';
n2 = n2';
P1 = P1';
P2 = P2';
Damage = Damage';

T = table(Bands,Diameter,BandWidth,n1,n2,P1,P2,Damage,...
'RowNames',Bands);
cellArrayOfTableToBeWritten = [cellArrayOfTableToBeWritten;
                          T.Properties.VariableNames; 
                           table2cell(T); 
                           blankrow];

I run the code and gave me error for Band(t) and fair enough, I don't know what's the correct way to print in a table, but why an error on Bands(t+1)?

'Conversion to double from cell is not possible.'

EDIT: what I'm trying to do is this:

Band = {'One';'Two';'Three';'Four';'Total'};

but now with a for loop since I don't know how many bands I'll have until I start the programme. After I create the table using a for loop I export it in an Excel file using xlwrite (not xlswrite bc I'm on Mac).

2
  • It looks like this code could be greatly optimised, it would be constructive if you actually described what you were trying to achieve. Also when you are asking here about an error, always include the entire, actual error message. Commented Dec 13, 2017 at 10:52
  • I edited my original post a bit. The error message is in its entirety bc it's not a 'red' one, it's the result of a try catch ME. Commented Dec 13, 2017 at 13:34

1 Answer 1

1

I assume when you are using

Bands(t) = {'Band %d',t}

You are expecting to assign the tth element of Bands to the value 'Band 1' for instance. What you're actually doing is assigning the tth element of Bands to the cell array {'Band %d', 1}. You need to use sprintf:

Bands(t) = {sprintf('Band %d', t)}; % >> output 'Band 1' when t=1
% or 
% Bands{t} = sprintf('Band %d', t);

By doing this, you should fix the issue down the line with assignment mismatches.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes you are assuming right. I already tried to do that myself before posting this but it gives me this error on that very line: Conversion to double from cell is not possible.
That would be because you have initialised your table to have the column variable type of double. You haven't shown how you initialised Bands so I cannot help you fix that. Please edit the question to include the initialisation. Instead of trying to explain your code, usually it's easier to post a simple input and expected output example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.