필터 지우기
필터 지우기

Error for textscan command

조회 수: 1 (최근 30일)
Punit Gandhi
Punit Gandhi 2015년 5월 7일
댓글: Guillaume 2015년 5월 7일
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

답변 (2개)

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

Guillaume
Guillaume 2015년 5월 7일
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
Punit Gandhi
Punit Gandhi 2015년 5월 7일
I am getting the following error
Error using plot
Vectors must be the same lengths.
Guillaume
Guillaume 2015년 5월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by