How to rename files and put them into structure?

Hello! I am doing a GUI in appdesigner.
With this line, I can open multiple files.
[file, path] = uigetfile('*.s2p','MultiSelect','on');
In the first image there are two files (example) i am opening.
When I open the files, I get this (second image). In this case I opened 3 files.
So, in my GUI I have an antenna array like this (third image). Now I am trying to assing the files to specific antennas.
For example, I want to say that the file 'sparameters_2,1_3,3.s2p' has the relationship between antenna 2,1 and antenna 3,3. And I want to have a struct with the relationship betwwen all the antennas. So when I open multiple files, my goal is to open every one which has numbers like 3,1_2,2, and to this this to have a struct. Exmaple:
A1 A2 file
1,1 1,2 sparameters_1,1_1,2
1,1 2,1 sparameters_1,1_2,1
...
10,1 3,4 sparameters_10,1_3,4
I don't know what to do to the files... Maybe rename them just to have the numbers... And then how can I assign them like in the example? I am a little lost, if you could give some clue, I would apreciate very much. Thanks!

 Accepted Answer

If I understand the situation, there is a limit on the number of antennas you can store in pairs in file names like that. I mean, there is no delimiter between the antenna indices in the antenna_*.s2p file names (unlike the sparameters_*.s2p file names, which use a comma as a delimiter). So say you had 11 antennas, then there would be no way to determine whether 'antenna_11123.s2p' meant antennas (1,11) and (2,3) or antennas (11,1) and (2,3). Your antenna matrix is 3-by-4, but you mention antenna (10,1) in your example, so I don't know whether this is a problem.
(It's also not completely clear to me that you need to use antenna_*.s2p file names at all, when you could select the corresponding sparameters_*.s2p files, which are uniquely decodable. But I'm going by what you showed in the files.png screen shot.)
The following will do what I think you want to do, which is to create an array of structs containing information about which pair of antennas correspond to which sparameters file. However, it will only work for a matrix of antennas of size up to 9-by-9 due to the limitation described above (you could go up to 10x10 and be uniquely decodable, but having each antenna index represented by a single digit makes it easier).
file = {'antenna_2133.s2p' 'antenna_3311.s2p' 'antenna_1112.s2p' 'antenna_1121.s2p'};
file_info = struct();
nums = regexp(file,'_(\d+)\.','tokens');
nums = [nums{:}];
for ii = 1:numel(file)
file_info(ii).A1 = sprintf('%c,%c',nums{ii}{1}([1 2]));
file_info(ii).A2 = sprintf('%c,%c',nums{ii}{1}([3 4]));
file_info(ii).file = sprintf('sparameters_%s_%s',file_info(ii).A1,file_info(ii).A2);
file_info(ii).antenna_file = file{ii}; % just in case you need this too
end
% show the array of structs file_info:
file_info
file_info = 1×4 struct array with fields:
A1 A2 file antenna_file
for ii = 1:numel(file_info)
file_info(ii)
end
ans = struct with fields:
A1: '2,1' A2: '3,3' file: 'sparameters_2,1_3,3' antenna_file: 'antenna_2133.s2p'
ans = struct with fields:
A1: '3,3' A2: '1,1' file: 'sparameters_3,3_1,1' antenna_file: 'antenna_3311.s2p'
ans = struct with fields:
A1: '1,1' A2: '1,2' file: 'sparameters_1,1_1,2' antenna_file: 'antenna_1112.s2p'
ans = struct with fields:
A1: '1,1' A2: '2,1' file: 'sparameters_1,1_2,1' antenna_file: 'antenna_1121.s2p'

7 Comments

Thank you for your answer. Actually, the ideia is that the user can chose the size of the array, só it can be 20×20 for example. I didn't decid the max size yet, but it has to be much more than 9×9.
My first idea was to press two buttons and chose just the file that relates them and it works. But I realize that if the antenna array is to big like 20×20, this process will be exhaustive. So I wanted a process more user friendly.
Then you will run into the problem with naming the antenna_* files like that. Is there a reason you cannot use the sparameters_* file names instead? If not, then you (or I) can adapt the code in my answer to build the struct array from those names instead, but the problem that antenna_11123.s2p is ambiguous will remain unless you prepend the numbers with zeros or use a delimiter, like a comma is used in the sparameters_ names. Probably it's best to use the same naming convention for both sets of files, if you can decide that.
I think I can use always the sparameters_*.s2p. But I don't know I to modify the code u write. Can you show me an example please? Thank you very much for your help
No problem. Here you go. This will work for any size antenna matrix as long as the sparameters_ file names have underscores and commas separating the antenna indices, like you have it now. (I put in one with 2-digit indices to show that it works for that case.)
(The struct array also includes the antenna_ file name, but that may not be useful since the antennas they refer to can be ambiguous - again, I recommend using the same format for the antenna file names as you use for the sparameters file names since the way it is now, multiple pairs of antennas can map to the same file name, e.g., (11,2) -> antenna_112.s2p and (1,12) -> antenna_112.s2p.)
file = {'sparameters_2,1_3,3.s2p' 'sparameters_3,3_1,1.s2p' 'sparameters_1,1_1,2.s2p' 'sparameters_6,12_11,7.s2p'};
file_info = struct();
nums = regexp(file,'_(\d+),(\d+)_(\d+),(\d+)\.','tokens');
nums = [nums{:}];
for ii = 1:numel(file)
file_info(ii).A1 = sprintf('%s,%s',nums{ii}{[1 2]});
file_info(ii).A2 = sprintf('%s,%s',nums{ii}{[3 4]});
file_info(ii).file = file{ii};
file_info(ii).antenna_file = sprintf('antenna_%s%s%s%s.s2p',nums{ii}{:}); % just in case you need this too
end
% show the array of structs file_info:
file_info
file_info = 1×4 struct array with fields:
A1 A2 file antenna_file
for ii = 1:numel(file_info)
file_info(ii)
end
ans = struct with fields:
A1: '2,1' A2: '3,3' file: 'sparameters_2,1_3,3.s2p' antenna_file: 'antenna_2133.s2p'
ans = struct with fields:
A1: '3,3' A2: '1,1' file: 'sparameters_3,3_1,1.s2p' antenna_file: 'antenna_3311.s2p'
ans = struct with fields:
A1: '1,1' A2: '1,2' file: 'sparameters_1,1_1,2.s2p' antenna_file: 'antenna_1112.s2p'
ans = struct with fields:
A1: '6,12' A2: '11,7' file: 'sparameters_6,12_11,7.s2p' antenna_file: 'antenna_612117.s2p'
Thak you very much for your help! It was precious! :)
I already understand, I will change the name of my files and separate all with commas.
Thank you again! :)
OK great! You're welcome!

Sign in to comment.

More Answers (0)

Asked:

on 19 Feb 2022

Commented:

on 20 Feb 2022

Community Treasure Hunt

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

Start Hunting!