How do you load an external datafile using uiopen and then rename workspace variable without copying it?

18 Ansichten (letzte 30 Tage)
There are a number of questions asking how to rename a workspace variable, but I haven't found a very satisfactory answer.
Here's use case for why I want to.
I often have large datafiles with long complex names that I've downloaded from some data source.
For example, I just downloaded this file: 'SHARADAR_INDICATORS_c41c38a0aaed171169bb790c5b4b459a.csv'
In some cases these files are gigabytes in size, eg very large csv files, or perhaps json files, etc.
When I drag/drop the filename from the Mac Finder window directly into the matlab command window, Matlab conveniently auto-creates a command to import the data, like:
uiopen('[path to data...]/Downloads/sharadar/SHARADAR_INDICATORS_c41c38a0aaed171169bb790c5b4b459a.csv',1)
Running this uiopen command creates a variable in memory called SHARADAR_INDICATORS_c41c38a0aaed171169bb790c5b4b459a.
This ain't the most besterest of names, to say the least. There is a way to change the imported variable name manually in the uiopen dialog.
Conveniently, the workspace editor has a nice right-click option to rename variables in the workspace. I presume that it does so without copying them.
I'd like a programatic way to rename a variable name, and thereby to get the data into the workspace with a reasoable name. For example, I'd like to be able to rename a workspace variable without copying it.
Here is some pseudocode of a few ways this could work:
filename = '[path to data...]/sharadar/SHARADAR_SF1_017f04a0d2ef7cc409f920be72167ada.csv';
data = betterUIOpen(filename); % existing uiopen does not allow outputs, though it can create a function with outputs
% or
betterUIOpen2(filename, 'data'); % specify the resultant variable name
% or
betterUIOpen3(filename);
renameVariableInWorkspaceFunction(filename,data);
% any of these would be better than copying the variable to a new variable, or renaming the file before importing it
Thank you

Antworten (1)

dpb
dpb am 18 Jul. 2022
Bearbeitet: dpb am 18 Jul. 2022
If you want control over the input process beyond the default action, take control...
[file,path]=uigetfile('[path to data...]/sharadar/SHARADAR_SF1_017f04a0d2ef7cc409f920be72167ada.csv');
data=readmatrix(fullfile(path,file));
I have to admit after 30+ years of MATLAB use I was unaware there was such a thing as uiopen(), "poofing" variables into the workspace in that manner is just too fraught with such difficulties as you outline, just never consider doing such.
Other than having to somehow get a name dynamically if you do that, the other way would be to pass the ugly/long name variable to a function in which the dummy argument is something reasonable.
I'd still recommend just avoid going that way from the git-go, though...

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by