Is there a way to save the filename used in a live script Import Data task?

2 vues (au cours des 30 derniers jours)
Jon
Jon le 4 Fév 2025
Commenté : Jon le 12 Fév 2025
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("filepath\filename.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.

Réponse acceptée

Umar
Umar le 4 Fév 2025

Hi @Jon,

To achieve your goal of retaining the filename as a variable in a MATLAB Live Script, you will need to modify how you import data and set up your filename handling. Below are steps and code snippets that should help you retain and use the filename variable effectively.

1. Manually Specify File Selection: Instead of relying solely on the Import Data task, you can prompt the user to select a file using uigetfile, for more information on this function, please click the following link.

https://www.mathworks.com/help/matlab/ref/uigetfile.html

This way, you can capture the filename directly into a variable.

   % Prompt user to select a CSV file
   [filename, pathname] = uigetfile('*.csv', 'Select a CSV file');
   if isequal(filename, 0)
       disp('User selected Cancel');
   else
       fullFilePath = fullfile(pathname, filename);
       disp(['User selected ', fullFilePath]);
   end

2. Read Data Using readtable: After obtaining the full file path, you can read the data into a table.

   % Import the data
   rawdata = readtable(fullFilePath);

https://www.mathworks.com/help/matlab/ref/readtable.html

3. Extract Components from Filename: With filename now available as a variable, you can manipulate it as needed. For example, if your filenames follow a specific format like serialnumber_testnumber_operator.csv, you can split it accordingly.

   % Remove file extension for further processing
   [~, name, ~] = fileparts(filename);  % Extracts 
   'serialnumber_testnumber_operator'
   % Split by underscore or any delimiter used in your naming convention
   parts = split(name, '_'); 
   serialNumber = parts{1};
   testNumber = parts{2};
   operator = parts{3};
   % Create a descriptive plot title
   plotTitle = sprintf('%s vs %s, %s %s', 'Y', 'X', serialNumber, testNumber);

4. Plotting with Descriptive Names:Now that you have the necessary components extracted from the filename, you can easily use them in your plots.

     % Example plotting (assuming X and Y data are defined)
     figure;
     plot(X, Y);
     title(plotTitle);
     % Save plot with descriptive filename
     saveas(gcf, sprintf('%s_%s_%s_plot.png', serialNumber, testNumber,   
     operator));

Here are some additional insights that I would like to share as well,

User Interface Features: Since you're utilizing sliders for user input ranges, ensure that these controls are appropriately tied to your dataset. This enhances usability for non-MATLAB users.

Batch Processing Consideration: If you're planning to process multiple files in one go, consider wrapping your import and processing logic in a loop that iterates over all selected files.

Live Script Limitations: While Live Scripts offer enhanced interactivity and visualization capabilities, keep in mind that certain traditional functionalities may be abstracted away. Always consider reverting to command-line scripts for more complex logic where necessary.

By following these steps and modifying how you approach file selection and data importing within MATLAB Live Scripts, you should be able to retain and utilize filenames as variables effectively while maintaining user-friendly functionality.

If you encounter any further challenges or need additional clarification on specific aspects of this process, feel free to ask!

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by