How can i modify import tool function with another script?

2 views (last 30 days)
1.sahne on 5 Jan 2017
Answered: Jordan Ross on 9 Jan 2017
My aim is to have a script that imports any numerical data (txt, csv, ascii,...) with the matlab import tool and then plots the values.
The data might look like this:
0 , 2
1 , 3
2 , 3
3 , 4
The thing is, I want the user to be able to save the import settings of the tool in order to use it again for similar data sets instead of defining the settings all over again.
There is the option to save the import tool with the chosen settings in a function but it requires the number of the rows where the data set starts:
function [U_V,I_A] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [U_V,I_A] = IMPORTFILE(FILENAME) Reads data from text file FILENAME for
% the default selection.
% STARTROW through ENDROW of text file FILENAME.
% Example:
% [U_V,I_A] = importfile('testdata_txt_1.txt',5, 19);
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2017/01/05 10:01:15
%%Initialize variables.
if nargin<=2
startRow = 5;
endRow = inf;
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%5s%7s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% 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, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
%%Close the text file.
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings 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>.*)';
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
catch me
%%Allocate imported array to column variable names
U_V = cell2mat(raw(:, 1));
I_A = cell2mat(raw(:, 2));
So My script would either need to look into this function and get its variable "startRow" or change the function in a way, that it doesnt ask for the Row Input anymore .
But how can I modify the import tool functiowith my own script? Can anyone help me here?
Thanks for your answers!

Answers (1)

Jordan Ross
Jordan Ross on 9 Jan 2017
Hello Linus,
As I understand, you want to have a script that imports any numerical data (txt, csv, ascii,...) with the MATLAB import tool and then plots the values.
You are right, you can use the import tool to generate a function to do the import. Additionally, "startRow" is a parameter that you can pass to the function so you can set this dynamically each time you call this function.
Also note that if you plan on reading different formats then you will also need a way to pass the format specified to this function. In the auto-generated function from the data import tool this variable is called "formatSpec".

Community Treasure Hunt

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

Start Hunting!

Translated by