Automatic file opening in MATLAB - matlab

I'm running a program on magnetometry. I have a file which contains over 10 text files, each containing the data (amplitude vs frequency) at a precise magnetic field value.
My program then reads each of those files, plotting the data, making a fit over those and then I use this fit to find the magnetic field depending on the distance of the frequency between 2 amplitude peaks (that's just theory, it's not necessary to understand this part).
All I want is some lines of code that would open all the files in the specified directory and let me use the datas
(i.e. data = importdata(filenames{i},delimiterIn,headerlinesIn);)
And later on I have a line that ask the user which data file he wants to open, and it gives back the magnetic field value.
So I need to use two folders : the one containing the data to create my fit and equations.
And the one containing whatever data file the user wants to open to find the magnetic field applied while getting data.

It isn't clear from the question if you want the user to do this interactively or not, so I assumed you do.
To select a folder you can use uigetdir , for example:
d = uigetdir('C:\');
will displays directories on the C: drive to select from, etc...
Similarly, to select all the files in that folder you can use dir. For example, if you want to pick out all the TXT files in a folder that the user selects:
d = uigetdir(pwd, 'Select a folder');
files = dir(fullfile(d, '*.txt'));

Related

Why am I getting "Unable to read file 'topo60c'. No such file or directory" error in Matlab?

Many of Matlab's Mapping toolbox examples require "topo60c" world map data. Here's an example
load topo60c
axesm hatano
meshm(topo60c,topo60cR)
zlimits = [min(topo60c(:)) max(topo60c(:))];
demcmap(zlimits)
colorbar
However, when I run the above script, Matlab displays a file not found error for "topo60c". Does anyone know why I'm getting this error? I have the Mapping toolbox installed, and it works with other Mapping sample code that doesn't reference that file.
In the acknowledgements section of the mapping toolbox docs there is a note about example data sources:
https://uk.mathworks.com/help/map/dedication-and-acknowledgment.html
Except where noted, the information contained in example and sample data files (found in matlabroot/examples/map/data and matlabroot/toolbox/map/mapdata) is derived from publicly available digital data sets. These data files are provided as a convenience to Mapping Toolbox™ users. MathWorks® makes no claims that any of this data is free of defects or errors, or that the representations of geographic features or names are up to date or authoritative.
You can open these folders from MATLAB (on Windows) using
winopen( fullfile( matlabroot, 'examples/map/data' ) )
winopen( fullfile( matlabroot, 'toolbox/map/mapdata' ) )
Or simply use the fullfile commands above to identify the paths and navigate there yourself.
I can see (MATLAB R2020b) the topo60c file within the first of these folders, which isn't on your path by default because it's within "examples" and not a toolbox directory:
So you could either:
Add this folder to your path so that MATLAB can see the file: addpath(fullfile(matlabroot,'examples/map/data'));
Reference the full file path to the data when running examples: load(fullfile(matlabroot,'examples/map/data/topo60c.mat'));
I would prefer option 2 to avoid changing the path.
Additionally, there is another note in the Raster Geodata section of the docs which details what that dataset should contain
https://uk.mathworks.com/help/map/raster-geodata.html
When raster geodata consists of surface elevations, the map can also be referred to as a digital elevation model/matrix (DEM), and its display is a topographical map. The DEM is one of the most common forms of digital terrain model (DTM), which can also be represented as contour lines, triangulated elevation points, quadtrees, octree, or otherwise.
The topo60c MAT-file, which contains global terrain data, is an example of a DEM. In this 180-by-360 matrix, each row represents one degree of latitude, and each column represents one degree of longitude. Each element of this matrix is the average elevation, in meters, for the one-degree-by-one-degree region of the Earth to which its row and column correspond.
Given that it's generated from publically available data anyway (ref the first docs quote) and you now know what data it represents (ref the 2nd docs quote), you could replicate some replacement data if really needed.

MATLAB - Specified all paths, but it's always opening to the last path?

sound_dirs={Ball, Daddy,Jeep, No, Tea_Pot};
The 5 variables in sound_dirs are path-char-vectors to 5 different sound folders.
data_structure is a cell of (length(num_sounds) row x 3 column cell
each row corresponds to a different sound
first column = directory name
second column = files struct for .wav files
third column = formant data
I want to be able to index into the files struct and do some processing on each .wav file of the same type, and then generate statistics on the group.
I use this method to get the '.wav' files
for i=1:num_sounds;
i_num_wavs=length(data_structure{i,2});
ith_file=data_structure{i,2};
for j=1:i_num_wavs;
[y Fs]= audioread(ith_file(j).name);
% end x 2 at the end of the processing
The problem:
The program I wrote always opens to the TeaPot Directory, which is the last directory. So when I call the ith term in the for loop, I somehow get TeaPot files and those don't match up.
I use this method to take off the last directory names to store in data_structure{i,1}
for i=1:num_sounds;
path=sound_dirs{i};
[path, fname, ext] = fileparts(path);
opendir = strcat(fname, ext);
words{i}=opendir;
end
Where is the logic wrong?

How to store all the data loaded from multiple files in one file after a for loop in matlab?

I have 47 different files for 47 locations. Each file contains 3000x1 data. I want to load all the files together. Then I want to check the unique values of each file (in this case I will have 47 sets of unique data sets) and want to save all the unique files (47 sets of unique data in one file. Then I want to check the probability. In this case I will have 47 sets of probability files for 47 sets of unique data files. Now I want to store all the 47 sets of probability files in another file. When I am using the programme below, I am only able to store one set of unique data and one set of probability data (i.e for 47th location only). How can I store for all the locations together in this case? Please guide. (I know there are some problems with the loop but I am not able to fix it)
for location=1:47;
load(['data_sets/data_loc_' num2str(location) , '_trial.mat'])
un_rssi= unique(RSSI_all);
normhist= hist(RSSI_all,size(un_rssi,1))/sum(hist(RSSI_all));
end
un_rssi = cell(47,1); % initialise for speed
normhist = cell(47,1);
for location=1:47;
load(['data_sets/data_loc_' num2str(location) , '_trial.mat'])
un_rssi{location,1}= unique(RSSI_all); % store in the cell
normhist{location,:}= hist(RSSI_all,size(un_rssi,1))/sum(hist(RSSI_all)).'; % need a column vector
end
un_rssi = cell2mat(un_rssi); % switch to a matrix
normhist = cell2mat(normhist);
dlmwrite('YourFile1.txt',un_rssi); % Write to file
dlmwrite('YourFile2.txt',normhist);
You can of course use whatever file writing function you like, I just used dlmwrite as an example.

MATLAB EEG signal processing - Channel location file

I'm trying the EEGLAB and FASTER plugins for MATLAB in order to do some processing for my EEG data, When trying to load the data file, I'm asked to choose the "channel location file", but I don't have that with my data, I was wondering if I can create it myself? And if so, How? I know that each channel in my data corresponds to a specific electrode, how can I write that in the location file? Thank you
You can do it by file or by code.
By file (I did not test it so it may not work):
create a text file with electrode names - one electrode per line, the order should be the same as in your file. Load the file through edit -> channel locations --> read locations (left bottom corner of the gui). Choose your text file and then use "look up locs" button to get corresponding locations on BESA or MNI head model.
By code and gui (should work well):
Create a variable with electrode names (have to be correct names in correct order):
elec_names = {'Cz', 'O1', 'O2', 'Fp1', 'Fp2'};
[EEG.chanlocs.labels] = deal(elec_names{:});
eeglab redraw;
Then use the edit -> channel locations --> look up locs option. Later you can type eegh in command window to get the command that would work on your computer.
This sounds like you are not really aware of how EEGLAB works.
From the EEGLAB wiki page on the topic of "Channel Location"
To plot EEG scalp maps in either 2-D or 3-D format, or to estimate
source locations for data components, an EEGLAB dataset must contain
information about the locations of the recording electrodes.
KEY STEP 5: Load the channel locations.
To load or edit channel location information contained in a dataset, select Edit > Channel locations.

Pop-up windows asking the user for input/save his work (MATLAB)

I am writing a program and I need some help. It starts by asking this question:
A = questdlg('What would you like to do?','Artificial Neural Network',...
'Train','Test','Exit','Exit');
Then depending what the use chooses it asks certain questions and do certain things
`if strcmp (A,'Train')
B = questdlg ('Would you like to create a new network or add to the already trained data?',...
'!','Create','Add','Exit','Exit');
if strcmp (B, 'Create')
if strcmp (B, 'Create')
%add as many text file as he wants to - need to figure out how I
%can extract the data from them though
[fname,dirpath]=uigetfile ('*.txt','Select a txt file','MultiSelect',...
'on');
elseif strcmp(B,'Add')
%choose what type is it
D = listdlg('PromptString','What colour is it?',...
'SelectionMode','single', 'ListString',...
{'Strawberry','Orange',...
'Chocolate','Banana','Rose'}, 'Name','Select Ice Cream',...
'ListSize',[230 130]);
%and then whatever choise he chooses it will feed it to the main
%function. For example if he chooses Orange then it will go the
%second part of the training, if it chooses Rose and the fifth
%one and so on.
else strcmp(B,'Exit')
disp('Exit')
end
So the thing I want help with is:
How can the user when he imports the txt files in Matlab use them in order to run the program? and
How can the user add more choices at the listdlg and when it will choose a choice then automatically it will go to the corresponding step of the code?
Any help would be appreciated!
Thanks!! :)
PS: Sorry for the long post!
with uigetfile etc. you only get the filename and path. But to get the data you have to load the file:
For mat-files use:
TMW: load mat-files
For other files use:
TMW: load data from file
To open a file in MATLAB, you can use uigetfile. To save a file, you can use uiputfile. This will open up standard file dialog boxex for opening and saving files. The result would be a cell array, and then use textscan to read the data from the individual files.
You should switch-case. On selecting one of the choices, you can train the neural network accordingly. The training preferably should be written in separate m files or different subfunctions for readability.