Extract Values from String with sscanf

조회 수: 7 (최근 30일)
Alex Perrakis
Alex Perrakis 2022년 5월 5일
댓글: Alex Perrakis 2022년 5월 10일
Hallo Guys and Gals,
i have following code:
clear
close all
addpath(genpath('../../Functions'))
%Parameters for GRR
appraisersCount = 4; gapCount =118; measurementsPerSample = 15;
amountOfLayers = 3;
% Choose Folder for Gap Analysis
URL = uigetdir('C:\_Daten\Messungen 3.5.2022\');
% tic
% Open URL
appraisers = dir(URL); appraisers(1:2) = [];
counter = 0;
% for a = 1:appraisersCount
% samplesURL = [URL '\' appraisers(a).name];
% samples = dir(samplesURL); samples(1:2) = [];
% measurementsURL = [URL '\' appraisers(s+2).name];
measurements = dir(URL); measurements(1:2) = [];
% folderspec = 'C:\_Daten\Messungen\WS\Teil 8_1\Teil 8';
% D = dir('C:\_Daten\Messungen\WS\Teil 8_1\Teil 8\*.csv');
rowscounter=41;
for s = 1:rowscounter
% measurementsURL = [URL '\' appraisers(s+2).name];
% measurements = dir(URL); measurements(1:2) = [];
% %
% for m = 1:measurementsPerSample
% counter=counter+1;
% measurementURL = [measurementsURL '\' measurements(m).name];
file=fullfile( URL, measurements(s,1).name);
% table=readtable(file);
fid = fopen( file );
%
wellenlaenge = fgetl(fid);
wellenlaenge = sscanf(wellenlaenge,'%s');
%wellenlaenge = strrep(wellenlaenge,' ',';');
wellenlaenge = strrep(wellenlaenge,',','.');
wellenlaenge = sscanf(wellenlaenge,'%f');
%
% dataColumns = length(frequencies)*2; %*2 cause data contain real + imag
stringData = fscanf(fid,'%s');
% stringData = strrep(stringData,',-1,',',"-1",');
% stringData = strrep(stringData,',1,',',"1",');
% stringData = strrep(stringData,',0,',',"0",');
% stringData = strrep(stringData,'","',' ');
% stringData = strrep(stringData,',"',' ');
% stringData = strrep(stringData,'",',' ');
% stringData = strrep(stringData,'"',' ');
stringData = strrep(stringData,',','.');
%
data = sscanf(stringData, '%f');
% %
% data = reshape(data,dataColumns,[]);
data = data';
reflect(s,:)=data(:,1:125);
% end
%end
end
And i am trying to read the first line of the .csv file i posted with fgetl, nevertheless for some reason it wants to start the values of the first line with 0. while the values are like 902.1, rund the code as it is and you understand what i mean.
  댓글 수: 10
Jan
Jan 2022년 5월 10일
@Alex Perrakis: Remember. that you find deleted message in Google's cache. Simply search for a block of text, e.g.
"Well in reflect I wanted to store the second line of .csv file which works. The"
and open the cached version of the page. Then you will find Stephen's answer:
[fid,msg] = fopen('2022-05-03 11-16-00.csv','rt');
assert(fid>=3,'%s',msg)
str = fgetl(fid);
fclose(fid);
vec = sscanf(strrep(str,',','.'),'%f')
Alex Perrakis
Alex Perrakis 2022년 5월 10일
Thanks man you're a champ

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by