Vectors must be same length error

I have been given this code but it skips out the first line of data, meaning I cannot plot it as the vectors are different sizes, and I don't understand why? Very limited knowledge of MATLAB, I'm just trying to get out the raw data that is used to make the graphs.
filename = sprintf('IMT1.txt', pwd, date, sample);
Th = importdata(filename);
trans = Th.data;
text = Th.textdata;
text = text(2:length(text),:);
epoch = text(:,1);
for j = 1:length(epoch)
trans_time(j) = str2num(epoch{j});
end
trans_time = trans_time.'/1e3;
figure(1)
plot(trans_time-min(trans_time), trans/max(trans))
xlabel('Time (s)')
ylabel('Normalised Intensity')

1 个评论

Please post the complete error message.
By the way, the first line is not complete:
filename = sprintf('IMT1.txt', pwd, date, sample);
You have 3 arguments, but the format string does not have any format operators. See:
sprintf('hello', 9)
sprintf('hello %d', 9)
sprintf('hello %s', 'Megan')

回答(1 个)

Stephan
Stephan 2019-3-2
编辑:Stephan 2019-3-2
Hi,
i suggest to make life easier by working with a table:
% import your txt-file as table
Th = readtable('IMT1.txt','MultipleDelimsAsOne',true,'Delimiter',...
{' ','\t'},'ReadVariableNames',false);
% The 4.th column is only tabluator - delete it
Th.Var4 = [];
% The names may not be correct - adapt it to your needs:
Th.Properties.VariableNames = {'epoch','trans_time','intensity'}
This piece of code imports your data to a table with formats:
double, duration, double
in the order of the 3 columns and looks like:
Th =
179×3 table
epoch trans_time intensity
__________ ____________ _________
1.5506e+09 11:21:10.234 51029
1.5506e+09 11:21:17.475 50973
1.5506e+09 11:21:24.719 50953
1.5506e+09 11:21:31.961 51055
1.5506e+09 11:21:39.201 50989
... lots of lines
Advantage:
str2num and friends are not needed and all columns are of the same length of rows.
Im pretty sure this solves your problem.
To perform calculations on table entries or to plot them use the dot-notation - for example:
Th.epoch = Th.epoch / 1000
plot(Th.trans_time, Th.intensity)
If you need more assistance regarding this question comment on this answer.
Best regards
Stephan

此问题已关闭。

产品

版本

R2018b

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by