Grab a file without writting full name

I want to grab a file that is always located in same folder that starts with the name "Target" and ends with ".hex" but in front of Target there is a version number that can change.
In the following line you can see I'm trying to use the " * " to pick up any file. But this doesn't work. Anyone can help me how to do this?
firmwareFile = [basePath{1} '..\binaries\Target*.hex'];

Answers (1)

Kevin Holly
Kevin Holly on 19 Oct 2022
Edited: Kevin Holly on 19 Oct 2022
Can you try this?
folder = [basePath{1},filesep,'binaries']; % I'm assuming this is the folder location
firmwareFiles = dir(fullfile(folder,'Target*.hex'))

11 Comments

Resulted in the following error:
"The following error occurred converting from char to struct: Conversion to struct from char is not possible."
What is basePath{1}?
I think the isse is that firmareFiles is now a structure.
basePath = extractBetween(thisScriptLocation, 1, find(thisScriptLocation == '\', 1, 'last'));
You can access the file names with:
firmwareFiles.name
Access the first one as such:
firmwareFiles(1).name
Still have the same error with the char to struct
What line is giving you the error?
folder = [basePath{1},filesep,'binaries']; % I'm assuming this is the folder location
Hmm...
I'm not sure how you are getting an error there. Are you sure that is the right line or is it written different in your code?
basePath = extractBetween('This is a test character array','a ',' character');
folder = [basePath{1},filesep,'binaries']
folder = 'test/binaries'
I found out the issue I added this extra line. And now the firmwareFIle has the whole path for the file
firmwareFiles = dir(fullfile(folder,'Target*.hex'))
firmwareFile = [firmwareFiles.folder,filesep,firmwareFiles.name]
Hello, how do I check if thie firmwareFiles is empty or has more than one file to throw an error?
To detect if it is empty:
isempty(firmwareFiles)
To detect how many files were detected:
size(firmwareFiles,1)

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Asked:

on 19 Oct 2022

Commented:

on 20 Oct 2022

Community Treasure Hunt

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

Start Hunting!