![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/988315/image.png)
Extract Values from String with sscanf
1 次查看(过去 30 天)
显示 更早的评论
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
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')
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!