Hi, i have a file like attached. I'd like to show each source name in each column of results (For example If I calculate mean value). Could you tell me how to do? I know I can use fprintf but don't know how Thanks, regards

5 comentarios

Stephen23
Stephen23 el 1 de Jun. de 2017
Editada: Stephen23 el 1 de Jun. de 2017
@Julio Martín: you uploaded a PDF, but a PDF is useless for us. We cannot import PDF data, so we cannot test any code using your PDF. No one will sit and copy out those values by hand, so if you want help then please upload the original text file.
Jan
Jan el 1 de Jun. de 2017
"Each source name in each coumn of results" - but what are the "results"? What is the connection to fprintf? Do you want to pint something to a file?
What do you want to achieve? Please explain this exactly.
Julio Martín
Julio Martín el 2 de Jun. de 2017
Hi, I have already changed the file. I mean when I calculate results (mean, max, etc.). If I have eight channel results, I'd like to show source name Hope I've explained better. Thanks, regards
Walter Roberson
Walter Roberson el 2 de Jun. de 2017
You have 8 numeric columns. Should we assume that each column is one channel? At the top you have 8 lines under the source name header: should we assume that those 8 lines are the channel names?
Will the files always have 8 source names and 8 columns?
Julio Martín
Julio Martín el 18 de Jun. de 2017
Hi yes, each column is one channel, and yes, those 8 lines are the channel names Thanks

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Jun. de 2017
filename = 'BES30_1.txt';
nvars = 8;
fid = fopen(filename, 'rt');
if fid < 0; error('Could not open file "%s"', filename); end
failed_step = false;
while true
thisline = fgetl(fid);
if ~ischar(thisline); failed_step = true; break; end %end of file
if strcmpi(thisline, '[SOURCE NAMES]'); break; end
end
if failed_step; fclose(fid); error('No [SOURCE NAMES] in file'); end
channel_names = cell(1, nvars);
failed_step = false;
for K = 1 : nvars
thisline = fgetl(fid);
if ~ischar(thisline); failed_step = true; break; end %end of file
if isempty(thisline); failed_step = true; break; end %unexpected blank line
channel_names{K} = thisline;
end
if failed_step; fclose(fid); error('Not enough column names in file'); end
failed_step = false;
while true
thisline = fgetl(fid);
if ~ischar(thisline); failed_step = true; break; end %end of file
if strcmpi(thisline, '[DATA]'); break; end
end
if failed_step; fclose(fid); error('No [DATA] in file'); end
fmt = repmat('%f', 1, nvars);
data = cell2mat( textscan(fid, fmt, 'CollectOutput', 1) );
fclose(fid);
data_mean = mean(data);
data_max = max(data);
data_min = min(data);
channel_name_width = max( cellfun(@length, channel_names) );
name_fmt = repmat( sprintf('%%%ds ', channel_name_width), 1, nvars );
name_fmt(end-1:end) = '\n';
data_fmt = repmat( sprintf('%%%.dg ', channel_name_width), 1, nvars );
data_fmt(end-1:end) = '\n';
fprintf(name_fmt, channel_names{:});
fprintf(data_fmt, data_min);
fprintf(data_fmt, data_max);
fprintf(data_fmt, data_mean);

2 comentarios

Julio Martín
Julio Martín el 19 de Jun. de 2017
I thought it was easier Thank you!
Walter Roberson
Walter Roberson el 19 de Jun. de 2017
If the number of lines before each major section is fixed then this could be made notably shorter. Also, part of the length is in error checking.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 1 de Jun. de 2017

Comentada:

el 19 de Jun. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by