How to process a csv file where numbers and text are at the same column?

5 vues (au cours des 30 derniers jours)
Nivodi
Nivodi le 22 Juil 2018
Commenté : Nivodi le 5 Sep 2018
Hello, everyone. I have the attached CSV file and there are some things that I have to do:
  1. create a table,
  2. read every text or number that they are, after the colon (:),
  3. read all the rows from 1-37 and
  4. transfer the variables names at the first row and the text and numbers to the second row meaning a table 2X37.
I kindly asking for help because I have totally 7400 files to process!!! Thank you in advance.
  2 commentaires
Walter Roberson
Walter Roberson le 22 Juil 2018
That appears to be the 1991 MSA/MAS spectral file format, as described at https://www.microscopy.org/resources/scientific_data/HMSAFileFormat-Presentation_2012_small.pdf
Can you output as .hmsa instead? If so then I see https://github.com/pyhmsa/pyhmsa-matlab which appears to be python code to convert .hmsa into matlab .mat files.
Nivodi
Nivodi le 23 Juil 2018
No, unfortunately, I can't.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 22 Juil 2018
That's not a CSV file. You might have to write a custom reader for it. Here's a start:
fullFileName = fullfile(pwd, 'test2_seq.csv')
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Read the remaining lines of the file.
fprintf('%s\n', textLine);
if contains(textLine, '#DATE', 'IgnoreCase', true)
colonLocation = strfind(textLine, ':');
semicolonLocation = strfind(textLine, ';');
DATE = strtrim(textLine(colonLocation + 2 : semicolonLocation(1)));
elseif contains(textLine, '#TIME', 'IgnoreCase', true)
elseif contains(textLine, '#OWNER', 'IgnoreCase', true)
elseif contains(textLine, '#NPOINTS', 'IgnoreCase', true)
elseif contains(textLine, '#NCOLUMNS', 'IgnoreCase', true)
elseif contains(textLine, '#XUNITS', 'IgnoreCase', true)
elseif contains(textLine, '#YUNITS', 'IgnoreCase', true)
elseif contains(textLine, '#DATATYPE', 'IgnoreCase', true)
elseif contains(textLine, '#XPERCHAN', 'IgnoreCase', true)
elseif contains(textLine, '#OFFSET', 'IgnoreCase', true)
elseif contains(textLine, '#OFFSET', 'IgnoreCase', true)
elseif contains(textLine, '#LIVETIME', 'IgnoreCase', true)
elseif contains(textLine, '#SIGNALTYPE', 'IgnoreCase', true)
% etc.
end
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
  15 commentaires
Walter Roberson
Walter Roberson le 25 Juil 2018
You can zip the file and attach the zip
Nivodi
Nivodi le 25 Juil 2018
Thank you, Walter! :))

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 26 Juil 2018
projectdir = '.'; %directory the files are in
dinfo = dir( fullfile(projectdir, '*.xsp') );
filenames = fullfile(projectdir, {dinfo.name});
nfiles = length(filenames);
table_so_to_speak = cell(nfiles, 1);
for K = 1 : nfiles
S = fileread( filenames{K} );
parts = regexp(S, '^#?(?<field>\w+)\s*: *(?<val>.*?)\r?$', 'names', 'lineanchors', 'dotexceptnewline');
table_so_to_speak{K} = [{parts.field}; {parts.val}];
end
However, I suspect you want more like
projectdir = '.'; %directory the files are in
dinfo = dir( fullfile(projectdir, '*.xsp') );
filenames = fullfile(projectdir, {dinfo.name});
nfiles = length(filenames);
table_so_to_speak = cell(nfiles+1, 1);
for K = 1 : nfiles
thisfile = filenames{K};
S = fileread( thisfile );
[~, basename] = fileparts(thisfile);
parts = regexp(S, '^#?(?<field>\w+)\s*: *(?<val>.*?)\r?$', 'names', 'lineanchors', 'dotexceptnewline');
if K == 1
table_so_to_speak(1,1:length(parts)+1) = [{'filename'}, {parts.field}];
end
table_so_to_speak(K+1,:) = [{basename}, {parts.val}];
end
  12 commentaires
Nivodi
Nivodi le 5 Sep 2018
Thank you very much both of you!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations 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