Why is the x-axis scale different from what I specified in the code?

My problem is about the plot of this code. I attached the text file I have to use;I want to plot the hour(2nd column) on the x-axes and the value(3rd column) on the y-axis. But when I plotted them the scale on x-axis is different from the original data: His range is from 00:00:17 to 00:01:17 instead of 05:00:07 .Why? Could someone help me? Thank you very much
% YourFilePath = 'C:\Matlab\valori2.txt';
fid = fopen(YourFilePath ,'rt');
Datafile = textscan(fid,'%s%s%s%s%s','Headerlines',2);
Value = Datafile{3};
Value = cellfun(@(x) textscan(x,'%s','Delimiter', ' ')',Value ,'UniformOutput',false);
Y = vertcat(Value{:});
X = cellfun(@transpose,Y,'UniformOutput',false);
Z = cellfun(@str2double,X,'UniformOutput',false);
Value1=cell2mat(Z);
Time (:,1)= Datafile{2};
Time1=cell2mat(Time);
fclose(fid);
plot(Value1,'*')
set(gca,'xticklabel',{Time1})

 Réponse acceptée

Replace:
set(gca,'xticklabel',{Time1})
with:
xt = get(gca, 'XTick')
set(gca, 'xtick',xt(1:end-1), 'xticklabel',Time1(xt(1:end-1)+1,:))
and it should plot as you want.

4 commentaires

Thanks!It works but now the problem is that the x-axis starts from 00:00:07 but is not correlated with the exact value: everything is shifted. This code work with 31 data but if I will have more data?
My pleasure!
The easiest way to do what you want is to use the datetick function.
First, delete (or comment out) these lines:
% plot(Value1,'*')
% xt = get(gca, 'XTick')
% set(gca, 'xtick',xt(1:end-1), 'xticklabel',Time1(xt(1:end-1)+1,:))
then insert these lines in their place:
Time2 = datenum(char(Time), 'HH:MM:SS');
plot(Time2, Value1, '*')
datetick('x', 'MM:SS')
xlabel('Time (min:sec)')
Everything should work as you want it to now.
Thank you very much!Now it's perfect

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by