Is there a way to save the filename used in a live script Import Data task?
I am new to live scripts and although the streamlined functionality seems like a real time-saver it also obfuscates how some of the functions are working. The data processing activity that I am currently working on is quite straightfoward; choose CSV file from whatever is available in the working folder, import the data and structure it to be processed, prompt user to set the range of interest using sliders (which is an awesome feature that saves having to sanitize my data before import), then process and plot the result. Everything works great, but I can’t figure out how to use the filename of what is being imported as a variable…
In the past, before I was exposed to live scripts and the built-in tasks, I just wrote my code to have a commented line for the user to enter the filename which meant I could chop that filename up into useful pieces assuming it was named accordingly (for example: serialnumber_testnumber_operator.csv). Having that nicely structured filename allowed me to batch process any arbitrary number of data sets from a folder and spit out plots that were named for each data file (think "Y vs X, SN011 Test 3") which was great.
Setting my code up as a live script and changing the view to "hide code" makes a beatifully simple screen that I can turn over to people who don’t know anything about MATLAB for them to process data without my involvement, but I need that filename variable so that I can get the plots saved properly with their descriptive names. I’ve been sort of beating my head against the wall here trying to figure out how to do that, so I figured I should try asking for help.
Here’s the trail of breadcrumbs that I have followed so far starting within the code that exists in the Import Data task:
From this block:
% Import the data
rawdata = readtable("filepathfilename.csv")
If I step into the readtable function it goes straight into the ImportOptions function to line 496:
function T = readtable(filename,opts,varargin)
if ~isa(opts,’matlab.io.ImportOptions’)
error(message(‘MATLAB:textio:io:OptsSecondArg’,’readtable’))
end
try
func = matlab.io.internal.functions.FunctionStore.getFunctionByName(‘readtableWithImportOptions’);
T = func.validateAndExecute(filename,opts,varargin{:});
catch ME
throw(ME);
end
end
During that time the filename exists as a variable, but as soon as it steps out of the ImportOptions block filename disappears. If anyone can help me understand how to save that variable, it would be greatly appreciated.I am new to live scripts and although the streamlined functionality seems like a real time-saver it also obfuscates how some of the functions are working. The data processing activity that I am currently working on is quite straightfoward; choose CSV file from whatever is available in the working folder, import the data and structure it to be processed, prompt user to set the range of interest using sliders (which is an awesome feature that saves having to sanitize my data before import), then process and plot the result. Everything works great, but I can’t figure out how to use the filename of what is being imported as a variable…
In the past, before I was exposed to live scripts and the built-in tasks, I just wrote my code to have a commented line for the user to enter the filename which meant I could chop that filename up into useful pieces assuming it was named accordingly (for example: serialnumber_testnumber_operator.csv). Having that nicely structured filename allowed me to batch process any arbitrary number of data sets from a folder and spit out plots that were named for each data file (think "Y vs X, SN011 Test 3") which was great.
Setting my code up as a live script and changing the view to "hide code" makes a beatifully simple screen that I can turn over to people who don’t know anything about MATLAB for them to process data without my involvement, but I need that filename variable so that I can get the plots saved properly with their descriptive names. I’ve been sort of beating my head against the wall here trying to figure out how to do that, so I figured I should try asking for help.
Here’s the trail of breadcrumbs that I have followed so far starting within the code that exists in the Import Data task:
From this block:
% Import the data
rawdata = readtable("filepathfilename.csv")
If I step into the readtable function it goes straight into the ImportOptions function to line 496:
function T = readtable(filename,opts,varargin)
if ~isa(opts,’matlab.io.ImportOptions’)
error(message(‘MATLAB:textio:io:OptsSecondArg’,’readtable’))
end
try
func = matlab.io.internal.functions.FunctionStore.getFunctionByName(‘readtableWithImportOptions’);
T = func.validateAndExecute(filename,opts,varargin{:});
catch ME
throw(ME);
end
end
During that time the filename exists as a variable, but as soon as it steps out of the ImportOptions block filename disappears. If anyone can help me understand how to save that variable, it would be greatly appreciated. I am new to live scripts and although the streamlined functionality seems like a real time-saver it also obfuscates how some of the functions are working. The data processing activity that I am currently working on is quite straightfoward; choose CSV file from whatever is available in the working folder, import the data and structure it to be processed, prompt user to set the range of interest using sliders (which is an awesome feature that saves having to sanitize my data before import), then process and plot the result. Everything works great, but I can’t figure out how to use the filename of what is being imported as a variable…
In the past, before I was exposed to live scripts and the built-in tasks, I just wrote my code to have a commented line for the user to enter the filename which meant I could chop that filename up into useful pieces assuming it was named accordingly (for example: serialnumber_testnumber_operator.csv). Having that nicely structured filename allowed me to batch process any arbitrary number of data sets from a folder and spit out plots that were named for each data file (think "Y vs X, SN011 Test 3") which was great.
Setting my code up as a live script and changing the view to "hide code" makes a beatifully simple screen that I can turn over to people who don’t know anything about MATLAB for them to process data without my involvement, but I need that filename variable so that I can get the plots saved properly with their descriptive names. I’ve been sort of beating my head against the wall here trying to figure out how to do that, so I figured I should try asking for help.
Here’s the trail of breadcrumbs that I have followed so far starting within the code that exists in the Import Data task:
From this block:
% Import the data
rawdata = readtable("filepathfilename.csv")
If I step into the readtable function it goes straight into the ImportOptions function to line 496:
function T = readtable(filename,opts,varargin)
if ~isa(opts,’matlab.io.ImportOptions’)
error(message(‘MATLAB:textio:io:OptsSecondArg’,’readtable’))
end
try
func = matlab.io.internal.functions.FunctionStore.getFunctionByName(‘readtableWithImportOptions’);
T = func.validateAndExecute(filename,opts,varargin{:});
catch ME
throw(ME);
end
end
During that time the filename exists as a variable, but as soon as it steps out of the ImportOptions block filename disappears. If anyone can help me understand how to save that variable, it would be greatly appreciated. live script, data import, filename MATLAB Answers — New Questions