Why is textscan suddenly not working anymore (using the App Designer) ?

1 view (last 30 days)
I am working with the App Designer to create an app to read csv files (it contains a lot of data (numbers and characters) but I only use 2 columns) and then plot a graph. It is a simple app with currently only two buttons ('load csv file' and 'load another csv file') and a graph.
My files are named like this: Total_120-200-40A_2017_BTSV_a.csv
Until today everything was working just fine. Without changing anything I get this error when trying to upload a file now: 'Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.' I tried using fopen but I have too many inputs.
Unfortunatley I have not found any information online to solve my problem on my own.
The following code is the code view for the button 'load csv file':
filename = uigetfile('*.csv');
delimiter = ';';
%%Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric text to numbers.
% Replace non-numeric text with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1));
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
% Converts text in the input cell array to numbers. Replaced non-numeric
% text with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\.]*)+[\,]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\.]*)*[\,]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData(row), regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains('.')
thousandsRegExp = '^\d+?(\.\d{3})*\,{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = strrep(numbers, '.', '');
numbers = strrep(numbers, ',', '.');
numbers = textscan(char(numbers), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch
raw{row, col} = rawData{row};
end
end
end
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%%Create output variable
M = cell2mat(raw);
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp R;
M(1,:)=[];
delta=[M(:,14)];
G=[M(:,10)];
plot(app.UIAxes, delta, G)
app.UIAxes.YScale = 'log';
Thank you already for any kind of help!

Answers (1)

Saurabh
Saurabh on 10 Sep 2024
The most probable cause is that the file you are attempting to open is not present. Verify that the file is in the folder you are currently in or a folder you have added to your MATLAB path, and that the file name is spelled correctly.
You may need to include the path to the file (absolute) along with the filename to open. You may want to consider using "fullfile" to construct that.
Levrage the below link to know more about "fullfile":
I Hope this was helpful.

Tags

Community Treasure Hunt

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

Start Hunting!