How to split 36x1 cell array into 36x3 cell array?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mohammed Qahosh
el 2 de En. de 2022
Comentada: Mohammed Qahosh
el 3 de En. de 2022
I have the attached text file which I need to split into 3 columns.
First column will be 36x1 with elements # {which I do not need}
Second column should contain the sentences up to :
Third column will contain the data { Kinetic Energy, Counts per Second .......}
I am using the following code, but I end up with 36x1 cell and I could not seperate the columns.
Thank you very much in advance for your help!!
clear all
close all
FileName=sprintf('file.txt');
fid= fopen(FileName, 'rt');
tline = fgetl(fid);
header = cell(0,1);
while ischar(tline)
header{end+1,1} = tline
tline = fgetl(fid);
if strcmp(tline,' Workfunction: ')== 1
sprintf('end')
break
end
end
header = split(header(:,1),' ');
fclose(fid);
0 comentarios
Respuesta aceptada
Walter Roberson
el 2 de En. de 2022
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/850945/file.txt';
S = webread(filename);
contents = regexp(S, '#\s+(?<header>[^:\n]+): +(?<value>[^\n]+)', 'names');
header = {contents.header}
values = {contents.value}
3 comentarios
Stephen23
el 3 de En. de 2022
Editada: Stephen23
el 3 de En. de 2022
@Mohammed Qahosh: I just tried it on R2013b and it worked without error (below). Make sure that you replace WEBREAD (which is clearly intended for importing the file that you uploaded here). with FILEREAD (which will load the file you have saved locally on your harddrive).
>> S = fileread('file.txt'); % <---------- you need FILEREAD here !!!!!!!!!!
>> contents = regexp(S, '#\s+(?<header>[^:\n]+): +(?<value>[^\n]+)', 'names');
>> header = {contents.header}
header =
Columns 1 through 6
'Created by' 'Comment Prefix' 'Output Meta Data' 'Energy Axis' 'Count Rate' 'Separate Scan Data'
Columns 7 through 13
[1x21 char] [1x21 char] [1x21 char] 'Error Bar' 'Operation Results' 'Time Zone Format' '# Group'
Columns 14 through 19
'# Region' 'Spectrum ID' 'Acquisition Date' 'Analysis Method' 'Analyzer Lens' 'Analyzer Slit'
Columns 20 through 25
'Scan Mode' 'Curves/Scan' 'Values/Curve' 'Dwell Time' 'Excitation Energy' 'Kinetic Energy'
Columns 26 through 32
'Pass Energy' 'Bias Voltage' 'Detector Voltage' 'Eff. Workfunction' 'Source' 'Comment' 'OrdinateRange'
>> values = {contents.value}
values =
Columns 1 through 11
[1x41 char] '#' 'yes' 'Kinetic Energy' 'Counts per Second' 'no' 'no' 'yes' 'no' 'no' 'yes'
Columns 12 through 19
'UTC' 'Fermi' 'Sb2Te3 (3)' '5' [1x21 char] 'XPS' 'WideAngleMode:400V' '3:0.1x30s\A:open'
Columns 20 through 30
'SnapshotFAT' '200' '1' '0.1' '50' '48.693' '50' '-0.5' '1550' '4.5' 'Monochromator'
Columns 31 through 32
[1x25 char] [1x23 char]
Más respuestas (1)
Voss
el 2 de En. de 2022
fid = fopen('file.txt');
data = fread(fid);
fclose(fid);
c_data = strsplit(char(data).',newline());
out = repmat({''},numel(c_data),3);
for i = 1:numel(c_data)
if isempty(c_data{i}) || c_data{i}(1) ~= '#'
continue
end
out{i,1} = '#';
idx = find(c_data{i} == ':',1);
if isempty(idx)
continue
end
out{i,2} = strtrim(c_data{i}(2:idx));
out{i,3} = strtrim(c_data{i}(idx+1:end));
end
disp(out);
1 comentario
Mohammed Qahosh
el 3 de En. de 2022
And sorry for late reply, I had to wait to try it on my office 2021 Matlab. As I am still using 2017 on my personal laptop and with this version still I get en error.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!