Could someone please explain to me how to take characters from a text file and put it in MATLAB?

2 ビュー (過去 30 日間)
So I've tried using fscanf and sscanf to take a group of words and numbers and put them into a cell array. An example of what I want in the array is "Wind data AA0, #2". I am new to MATLAB, so I've had problems getting the words I see in my text file to MATLAB using the right code. Could someone give me an example of how this is done? My attempt:
>> fileid = fopen('myfile.txt');
>> a = fscanf(fileid,'%g %g',[35 inf]) % I need only to row 35 % don't understand "%g" part
>> a = a'; % I saw that I might need to transpose it
>> fclose(fid)
  5 件のコメント
Tony Pate
Tony Pate 2016 年 6 月 8 日
I figured out that a textscan would be more appropriate for creating a cell like I want.
>> fileID = fopen('MLsensLiftStart.txt','r');
>> Intro = textscan(fileID,'%s',35,'Delimiter','\n');
Using this code, I got what I needed out of the file. I have one more question, though if anyone could please help. I need to create a structure array that organizes my cell data. It is all being stored in a 35x1 cell, but I need each of the 35 rows to have individual names and be easily seen when I open the cell.
per isakson
per isakson 2016 年 6 月 8 日
編集済み: per isakson 2016 年 6 月 8 日
The file, MLsensLiftStartexample.txt, is uploaded twice. The two copies are identical(?). It contains 13 rows and the longest is 7060 bytes. It's tab delimited. It contains 1(row header) + 72(data) columns.
&nbsp
  • "I need to create a structure array" &nbsp Structure field names may be created based on the row headers in the first column. (Alternatively, the row headers may be used as key values of a containers.Map object.)
  • I imagine that you want a scalar, a&nbsp<1x1 struct>, structure. However, a&nbsp<1x72 struct> is an alternative.
  • "individual names and be easily seen" &nbsp asks for a table object.
Describe the structure you want.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 6 月 8 日
This code will read in the whole file (well, it works on your sample file):
fid = fopen('MLsensLiftStartexample.txt','rt');
hdr = fgetl(fid);
txt = fgetl(fid);
pos = ftell(fid);
N = numel(regexp(fgetl(fid),'[^\t]+','match'));
fmt = repmat('%s',1,N);
fseek(fid,pos,'bof');
C = textscan(fid,fmt,'Delimiter','\t');
fclose(fid);
C = horzcat(C{:});
N = str2double(C);
Have a look at the variable C: all of the data is there, and the equivalent numeric in N (where appropriate).
  1 件のコメント
James Chukwuemeka Agada
James Chukwuemeka Agada 2020 年 4 月 28 日
Would this be able to work with a gcode text file?
Trying to extract the X Y Z I J values from it
attached the txt file

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by