How to specify a path to a file in MATLAB Drive when using MATLAB Online

Using MATLAB Online I have 3 apps and some image files in MATLAB Drive. Two of the apps require the image files. The files are in the folder Images, which has several sub-folders, like 'BinaryImages'. When MATLAB Drive is my current folder, I'm unable to access any of my images with, for example, imread('Images\BinaryImages\binary1.bmp' ). I'm simply told it's a non-existent file. I've tried using the full path on my desktop: C:\Users\Stuart Smith\MATLAB Drive\Images\BinaryImages\binary1.bmp' , but this didn't work either. So what is the correct way to access my files?

1 Comment

Install MATLAB Drive Connector
MATLAB® Drive™ Connector provides an easy way to manage your MATLAB Drive files on your local computer. With the Connector, files synchronize automatically between MATLAB Drive online and your local MATLAB Drive when you have the Connector running.Install Instructions
To install MATLAB Drive Connector, follow these steps:
  1. Start the installer using one of these methods:
  • From within MATLAB — Click the MATLAB Drive button to start the installer.If you do not see the MATLAB Drive button in the Current Folder toolbar, right-click the toolbar and select Customize. Then, in the MATLAB Toolbar Preferences Controls section, select the check box for the MATLAB Drive control and select OK. MATLAB adds the button to the toolbar.
  • From the web — Go to the Connector download page and select Install for Windows, Install for Mac, or Install for Linux. Installing from the web does not require you to have MATLAB installed.
  1. Configure the location of your MATLAB Drive folder and your MATLAB Drive settings.NoteSetting the location of your MATLAB Drive folder to a location on a network drive is not supported. This is because MATLAB Drive is unable to sync files and folders in a network location.
  2. Click Finish to complete setup and synchronize your MATLAB Drive files between your computers and online accounts. To change any of your selections, click Back.
When the installer finishes, MATLAB Drive is ready for use on this computer. See Access Files in Your MATLAB Drive for instructions on accessing MATLAB Drive.

Sign in to comment.

 Accepted Answer

MATLAB Online is Linux based. The directory separator is / not \ .
If you use fullfile() you do not need to know the character.
Note: Windows is fully able to use / as the character except in the old command shell.

More Answers (2)

Are you using MATLAB online?
My suggestion is to add the images folder and subfolders to your path. Right click on the folder and select Add to path > Selected folders and subfolders. Then you can just call the images by their names.
If you want to use the path, try using a relative path. This goes up one folder level, then follows the specified path to binary1.bmp.
imread('../Images/BinaryImages/binary1.bmp')

3 Comments

Once the files are added to the path in Matlab Online, you can access the files IF you enclose just the filename in double quotes (contra the usual Matlab syntax), e.g., img=imread("somefile.bmp"); You need not be in the folder where somefile.bmp resides. It appears that if you're one or more levels above the folder containing your file, MATLAB will find it. The downside is that if subfolders contain files of the same name, MATLAB will (I'm guessing) find the first instance it encounters on its way down the path list. So it looks like I will have to change the filenames in at least one folder so that those files will be accessible. Thanks to Cris LaPierre to pointing me in the right direction.
You should be able to use either single or double quotes.
I did think of a third way to do it. You can programmatically change your current folder just before loading the file, then change it back. That might look like this (borrowed from one of my scripts).
% Capture current folder
here=cd;
% Change current folder for loading
cd('/MATLAB Drive/MobileSensorData/');
load drop4.mat
% Change back to the original folder
cd(here);
The only thing to be aware of here is that, if there is an error during loading, your current folder will not have changed back.
I must have had a typo or something wrong when I tried this last night so I didn't suggest it. However, now that I'm awake I was also able to successfully use the full MATLAB Online path.
load('/MATLAB Drive/MobileSensorData/drop4.mat')

Sign in to comment.

With the release of R2021a you can now use the new matlabdrive function combined with fullfile to construct a path to files in your MATLAB Drive that will work in both MATLAB Online and on the desktop.
For example in this case, the following could work:
imread(fullfile(matlabdrive, 'Images', 'BinaryImages', 'binary1.bmp'))
I hope that helps in the future!

3 Comments

Nice!
A couple of weeks ago I started using a deliberate structure in my naming of files and directories that I store on MATLAB drive, to make it easier to move between desktop and Online. For example if I had a file binary1.bmp for this current Question, I would store it as
imread(fullfile(matlabdrive, '736', 'q736732', 'binary1.bmp'))
The 736 is the leading digits of the Question number, then directory is 'q' followed by the Question number. I put a q in front of it because sometimes I just put a file directly at the 736 level, such as
fullfile(matlabdrive, '736', 'q736732.m')
and either way, asking for a directory of q736732* will tell me what I stored with respect to 736732 .
Ah, MATLAB Online is only at R2020b at the moment, so this does not work for MATLAB Online yet.
And it turns out that the Run feature of Answers is not connected to our personal MATLAB Drive, so we would have to use a shared link to use from the Run feature. But that has problems too...
imshow('https://drive.matlab.com/sharing/901abb7d-27ae-4e2b-9e76-be269b351d3e/q770137.jpg');
Error using images.internal.getImageFromFile (line 13)
Cannot find the specified file: "https://drive.matlab.com/sharing/901abb7d-27ae-4e2b-9e76-be269b351d3e/q770137.jpg".

Error in images.internal.imageDisplayParseInputs (line 75)
images.internal.getImageFromFile(common_args.Filename);

Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Hi Walter,
Sorry for any confusion - MATLAB Online has now updated to R2021a :)
As you demonstrated, a MATLAB Drive sharing link cannot be used directly to load a file within MATLAB - thank you for the feedback.
Best wishes,
Gareth

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!