matlabtext-files

reading decimal number with GUI matlab


I am developing a user interface using MATLAB which allows to browse and load a text file and display some curves. I am facing a problem, my file text is a set of decimal number, MATLAB is reading those numbers as two columns.

This is an example: you find here the file that I am working on:

enter image description here

After runing this code:

[filename pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname,filename);
text = fileread(fullpathname); %reading information inside a file
set(handles.text6,'string',fullpathname)%showing full path name
set(handles.text7,'string',text)%showing information
loaddata = fullfile(pathname,filename);
xy = load(loaddata,'-ascii','%s');
t = xy(:,1);
i = xy(:,3);
handles.input1 = i;
handles.input2 = t;
axes(handles.axes1);
plot(handles.input1,handles.input2)

the curves looks so strenge, so I checked the result of xy= load(loaddata,'-ascii') using command window and here the problem appears!

enter image description here

So I have now 12 columns instead of 6! can you help me please? I tried with strrep(data,',','.') but it doesn't work!


Solution

  • Since you are using commas, for your radix point, you will want to first load in the entire file as a string, replace the , with . and then you can use str2num to convert the entire file to a numeric array

    % Read the entire file into memory
    fid = fopen(loaddata, 'rb');
    contents = fread(fid, '*char')';
    fclose(fid);
    
    % Replace `,` with `.`
    contents = strrep(contents, ',', '.');
    
    % Now convert to numbers
    data = str2num(contents);