Error for textscan command

1 view (last 30 days)
Punit Gandhi
Punit Gandhi on 7 May 2015
Commented: Guillaume on 7 May 2015
Hello,
I have textfile, which has 3 columns of 73 rows each. I want to plot it on the graph. I am using the following command for the same
fid=fopen('error10.txt');
s=textscan(fid,'%g %g %g');
fclose(fid);
x1=s{1};
x2=s{2};
x3=s{3};
y=-1:0.05:1;
plot(x1,y,x2,y,x3,y);
But when I use this command I get the following error
Error using textscan
Badly formed format string.
How do I solve this. I tried different format:%f, %e gave the same error. %d gave output in 0, 1 and -1. My textfile is as follows:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
-0.168681 0 -0.0293355
-0.169835 0 -0.0335232
-0.170417 0 -0.0293355
0.014163 0 -0.0162582
0.0143137 0 -0.0229441
0.0148041 0 -0.0162582
0.194393 0 0.0121311
0.195878 0 0.0121311
0.19889 0 0.0121311
-0.0634259 0 -0.00827191
0.0101036 0 -0.000894309
0 0 -0.00827191
0 0 0.00483092
0 0 0.0174881
0 0 0.00483092
0 0 0.0189944
0 0 0.00791766
0 0 0.0189944
0 0 0.00214116
0 0 0.00686661
-0.35176 0 0.00686661
-0.519225 0 -0.003751
-0.533674 0 -0.00777244
-0.539253 0 -0.00721154
0.0526248 0 -0.00634029
0.051712 0 -0.0120258
0.0512508 0 -0.00634029
0.0274714 0 0.00128514
0.0276167 0 0.00128514
0.0280894 0 0.00128514
0.00590875 0 -0.00868141
0.0076921 0 -0.00868141
0.0112886 0 0.000324675
0.0206031 0 -0.000328407
0.075283 0 -0.0512511
0 0 -0.0316681
0 0 -0.0701192
0 0 -0.0701192
0 0 -0.0322723
0 0 -0.0473929
0 0 -0.0530726
0 0 -0.0473929
0 0 -0.0101986
0 0 -0.0101986
0.42825 0 -0.0138587
0.0198806 0 -0.027907
0.0109786 0 -0.0169912
0.00749787 0 -0.027907
0.00576285 0 -0.0755837

Answers (2)

Ingrid
Ingrid on 7 May 2015
s=textscan(fid,'%f%f%f');
worked on the data you provided

Guillaume
Guillaume on 7 May 2015
The simplest way to read that file would be with load:
X = load('error10.txt');
y = -1 : 0.05 : 1;
plot(X(:, 1), y, X(:, 2), y, X(:, 3), y);
  2 Comments
Punit Gandhi
Punit Gandhi on 7 May 2015
I am getting the following error
Error using plot
Vectors must be the same lengths.
Guillaume
Guillaume on 7 May 2015
This has nothing to do with your original question. The problem is that the y is your example does not have the same number of elements as the number of rows in your textfile.
Only you know how to generate that y. One possible way:
y = linspace(-1, 1, size(X, 1)); %no idea if it's appropriate or not.

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!