How to split 36x1 cell array into 36x3 cell array?

4 vues (au cours des 30 derniers jours)
Mohammed Qahosh
Mohammed Qahosh le 2 Jan 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);

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Jan 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}
header = 1×32 cell array
{'Created by'} {'Comment Prefix'} {'Output Meta Data'} {'Energy Axis'} {'Count Rate'} {'Separate Scan Data'} {'Separate Channel Data'} {'External Channel Data'} {'Transmission Function'} {'Error Bar'} {'Operation Results'} {'Time Zone Format'} {'# Group'} {'# Region'} {'Spectrum ID'} {'Acquisition Date'} {'Analysis Method'} {'Analyzer Lens'} {'Analyzer Slit'} {'Scan Mode'} {'Curves/Scan'} {'Values/Curve'} {'Dwell Time'} {'Excitation Energy'} {'Kinetic Energy'} {'Pass Energy'} {'Bias Voltage'} {'Detector Voltage'} {'Eff. Workfunction'} {'Source'} {'Comment'} {'OrdinateRange'}
values = {contents.value}
values = 1×32 cell array
{'SpecsLab Prodigy, Version 4.84.1-r101483 '} {'#'} {'yes'} {'Kinetic Energy'} {'Counts per Second'} {'no'} {'no'} {'yes'} {'no'} {'no'} {'yes'} {'UTC'} {'Fermi'} {'Sb2Te3 (3)'} {'5'} {'12/02/21 12:48:01 UTC'} {'XPS'} {'WideAngleMode:400V'} {'3:0.1x30s\A:open'} {'SnapshotFAT'} {'200'} {'1'} {'0.1'} {'50'} {'48.693'} {'50'} {'-0.5'} {'1550'} {'4.5'} {'Monochromator'} {'circular polarization "-1'} {'[-15.000000, 15.000000]'}
  3 commentaires
Stephen23
Stephen23 le 3 Jan 2022
Modifié(e) : Stephen23 le 3 Jan 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]
Mohammed Qahosh
Mohammed Qahosh le 3 Jan 2022
Yes it does work even with R2017b.
Thank you @Stephen

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 2 Jan 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);
{'#'} {'Created by:' } {'SpecsLab Prodigy, Version 4.84.1-r101483'} {'#'} {0×0 char } {0×0 char } {'#'} {'XY-Serializer Export Settings:'} {0×0 char } {'#'} {'Comment Prefix:' } {'#' } {'#'} {'Output Meta Data:' } {'yes' } {'#'} {'Energy Axis:' } {'Kinetic Energy' } {'#'} {'Count Rate:' } {'Counts per Second' } {'#'} {'Separate Scan Data:' } {'no' } {'#'} {'Separate Channel Data:' } {'no' } {'#'} {'External Channel Data:' } {'yes' } {'#'} {'Transmission Function:' } {'no' } {'#'} {'Error Bar:' } {'no' } {'#'} {'Operation Results:' } {'yes' } {'#'} {'Time Zone Format:' } {'UTC' } {'#'} {0×0 char } {0×0 char } {'#'} {'Group:' } {'Fermi' } {'#'} {0×0 char } {0×0 char } {'#'} {'Region:' } {'Sb2Te3 (3)' } {'#'} {'Spectrum ID:' } {'5' } {'#'} {'Acquisition Date:' } {'12/02/21 12:48:01 UTC' } {'#'} {'Analysis Method:' } {'XPS' } {'#'} {'Analyzer Lens:' } {'WideAngleMode:400V' } {'#'} {'Analyzer Slit:' } {'3:0.1x30s\A:open' } {'#'} {'Scan Mode:' } {'SnapshotFAT' } {'#'} {'Curves/Scan:' } {'200' } {'#'} {'Values/Curve:' } {'1' } {'#'} {'Dwell Time:' } {'0.1' } {'#'} {'Excitation Energy:' } {'50' } {'#'} {'Kinetic Energy:' } {'48.693' } {'#'} {'Pass Energy:' } {'50' } {'#'} {'Bias Voltage:' } {'-0.5' } {'#'} {'Detector Voltage:' } {'1550' } {'#'} {'Eff. Workfunction:' } {'4.5' } {'#'} {'Source:' } {'Monochromator' } {'#'} {'Comment:' } {'circular polarization "-1' } {'#'} {'OrdinateRange:' } {'[-15.000000, 15.000000]' }
  1 commentaire
Mohammed Qahosh
Mohammed Qahosh le 3 Jan 2022
Thank you @Benjamin It works.
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by