Help me check what is wrong with my code

I want to print a table from a for loop with fprintf but when i run the code, it gives error
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in (line 47)
fprintf('%5s\t\t %5s\t\t %5s\t\t %5s\n',[data;t;time_for_iteration;s_new]);
this is the code with error below
fprintf('data\t\t t\t time_for_iteration\t s_new');
fprintf('%5s\t\t %5s\t\t %5s\t\t %5s\n',[data;t;time_for_iteration;s_new]);
data is 1x7 class double, t is 1x1 class double, time_for_iteration is 1x1 doubles_new is 1x1 double.
the remaining code is
[data]=([-1 2 -4 4 2 3 2]);
positive test algorithm
h=4; %threshold
s=0;
time_for_iteration=0;
for t=2:numel(data) %start time
s_new= s + data(t); %cumulative sum up to current value
if s_new > h %if output is greater than the specified threshold
%sound the alarm
detect_tim=t(s_new > h); %store the detection time
disp(['The detection time= ', num2str(detect_tim),'']) %display the detection time
% counter for algorithm
if s_new < 0
time_for_iteration=0;
else
time_for_iteration= time_for_iteration + 1;
end
% stop and reset algorithm
continue %to re-start algorithm
end
v=s_new;
end
Thank you

5 commentaires

Walter Roberson
Walter Roberson le 13 Août 2018
Why are you trying to use a %s format for numeric data?
What is your expected output when one of your data values has 7 components but the other three have only one component each? Are you expecting that the inputs that are scalar inputs will be automatically duplicated on the remaining 6 lines?
Rik
Rik le 13 Août 2018
In addition to Walter; generally, you should try to let the number of inputs match the number of identifiers, so you shouldn't need those brackets in this context.
Folakemi Omotoye
Folakemi Omotoye le 13 Août 2018
i included it in the loop (it was outside the loop in the code i posted due to error when i was spacing it) so it will output the corresponding values of the variable for each of the iteration.
I eventually figured how what was wrong. my vectors were row vectors. I need to convert them to column vector, but it outputs all the values of data first before the rest and i want it to output them in a table, such that the corresponding values of t,time_for_iteration for the corresponding value of data. The output it's giving is:
data t time_for_iteration s_new-1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 2.000000
0.000000 2.000000 -1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 3.000000
0.000000 -4.000000 -1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 4.000000
0.000000 4.000000 -1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 5.000000
0.000000 2.000000 -1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 6.000000
0.000000 3.000000 -1.000000 2.000000 -4.000000 4.000000
2.000000 3.000000 2.000000 7.000000
0.000000 2.000000
it outputs the whole of data before the rest.
fprintf('%f\t%f\t%f\t%f\n', [var1(:), var2(:), var3(:), var4(:)].' );

Réponses (0)

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by