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!