Convert Cell Arrays Containing Cell Arrays to separate columns

1 Ansicht (letzte 30 Tage)
I am trying to import specific number of lines from an output text file. I have used the textscan command to import the required lines. The imported data consists of 1000x1 cells (see the figure below). Any idea how to convert these cells into three separate columns?
% open the file
fid=fopen('dbe-ew-transient analysis - 5% critical damped.f06');
% set linenum to the desired line number that you want to import
linenum = 165164;
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
C = textscan(fid,'%s',1000,'delimiter','\n', 'headerlines',linenum-1);
M = C{:};
Thanks in advance.
  3 Kommentare
Mahmoud Madany
Mahmoud Madany am 6 Jan. 2022
The main problem is that the output file contains both numerical and text characters. So the specific range defined above is to import numeric data only from the file.
Regarding the READTABLE or READMATRIX, I struggled with the file format, i.e., "*.f06", which is not pupular, and to define a specific range of data.
I tried to CollectOutput option in textscan, where a value of zero is assign, but the data is still imported into a single column.
*Attached a sample of the imported file (I changes the extension to *.txt as attachment option does not support *.f06 files).
Stephen23
Stephen23 am 6 Jan. 2022
"I struggled with the file format, i.e., "*.f06", which is not pupular"
The file extension is irrelevant, you just need tell them that it is a text file using the option: "FileType","text":

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 6 Jan. 2022
The comments in the code suggest how to do this. Using the attached test file with some header lines to skip:
fid = fopen('test.txt');
% set linenum to the desired line number that you want to import
linenum = 4;
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
C = textscan(fid,'%f %f %f',1000,'delimiter','\n', 'headerlines',linenum-1);
fclose(fid);
C = cell2mat(C)
C = 5×3
1.0e+04 * 0.0001 1.0002 0.0000 0.0002 1.0002 0.0000 0.0003 1.0002 0.0000 0.0004 1.0002 0.0000 0.0005 1.0002 0.0000

Weitere Antworten (1)

Chunru
Chunru am 6 Jan. 2022
fn ='dbe-ew-transient analysis - 5% critical damped.f06';
linenum = 165164;
M = readmatrix(fn, 'Range', 165164+":"+999);

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by