How to use text file data as input to matlab code?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello, I need to use data in text file as a input to my matlab code. The data format in text file is as follows(each row has 4 columns: 1 for time, 2 for st. number, 3&4 are velocity components), Sample data:
Time St. DA DB
0 33 38.55 -10.87
2 33 37.35 -84.84
4 10 37.22 -62.29
6 43 37.39 -41.00
8 43 37.00 -19.75
I need to compare the time and st. number value with the values in my matlab code and pick 3&4 accordingly for further analysis. Any inputs are welcome.
채택된 답변
Try using textscan. You should read the documentation and pay attention to the option HeaderLines, and how FormatSpec is defined.
It is also important to use fopen and fclose, something like this:
fid = fopen('mydata.txt','rt');
C = textscan(fid,'%s %s %f32 %d8 %u %f %f %s %f');
fclose(fid);
This might be interesting to read too:
댓글 수: 8
Thank you!
Please do try it. If this solves your problem, you can click "Accept" for this answer. If the problem is unresolved, please explain what is not working.
Hey I am able to display rows bur not able to access exact value. I got 2 errors as follows.
1. fid = fopen('projectdata.txt','rt');
C = textscan(fid,'%n %n %n %n');
fclose(fid);
celldisp(C)
disp (C(1,1))
ans: [385x1 double]
But I need to access value at that location. How to get that value displayed?
2. fid = fopen('projectdata.txt','rt');
C = textscan(fid,'%n %n %n %n');
fclose(fid);
celldisp(C)
if C(1,1)==40
disp(C(1,2))
end
Error: Undefined function 'eq' for input arguments of type 'cell'.
So you might have something a bit like this:
fid = fopen('temp.txt','rt');
C = textscan(fid,'%f%f%f%f', 'HeaderLines',1);
fclose(fid);
Then you can access the data in C like this:
C{1} % The first column of data
...
C{4} % The fourth column of data
You could even merge all of the data into one numeric array, which would probably be easier to works with:
C = [C{:}];
How can I access a specific element? For example : 4th row and 2nd column
Is there a specific syntax to do that?
Because I am still not able to do operation like,
if C(1,1)==40
disp(C(1,2))
end
This is because I am not able to access the number at that position and instead I am accessing the ans '[384x1 double]'
Is there any way to solve this?
If you try the code that I recommended at the bottom of my last comment
C = [C{:}];
then you will end up with a simple numeric matrix, and you can use the indexing as you show in your comment:
if C(1,1)==40
...
end
FYI: the function textscan returns a cell array. You can read about cell arrays and how to index into them here .
Thank you so much!
If you want a table instead of a cell array, you can simply do
t = readtable(filename);
No need for fopen(), a format string, and fclose. I think it's a lot simpler but it requires release R2013b or later.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
