How to read '.m' files which is already saved in matlab

46 views (last 30 days)
I am just find the year by year nanmaean and saved that script by creating a new folder.Now i want to plot polynomial curve fitting using these nanmean values corresponding to the years .But i couldn't read that all seperate scripts together to plot that . For this am just used the following code, only showing the number of files. Folder= path upto new folder , File= dir('*.m')
  4 Comments
Stephen23
Stephen23 on 20 Jun 2023
Edited: Stephen23 on 20 Jun 2023
"How to read '.m' files which is already saved in matlab"
Note that:
  • .M files store code, and can be run (scripts) or called/evaluated (functions).
  • .MAT file store data, and the data can be imported.
Are you really storing data in .M files? Or are they scripts for running? Your question does not make this clear.
Jincy
Jincy on 20 Jun 2023
I am just save the scripts of 21 years seperately in a new folder to find the nanmean of indian using shapefile

Sign in to comment.

Answers (2)

Diwakar Diwakar
Diwakar Diwakar on 19 Jun 2023
If you will provide the .m file then we can able to give you the propoer answer with code. meanwile try the below code. may be it will help you.
% Step 1: Create a folder to store the '.m' files
% Step 2: Save each script as a separate '.m' file in the folder
% Step 3: Read the '.m' files
folder = 'path_to_folder'; % Replace 'path_to_folder' with the actual path to your folder
files = dir(fullfile(folder, '*.m'));
% Step 4: Loop through each file and calculate the nanmean values
nanmeans = [];
for i = 1:length(files)
file = fullfile(folder, files(i).name);
run(file); % Execute the script
nanmeans = [nanmeans, nanmean]; % Store the nanmean value
end
% Step 5: Plot the polynomial curve fitting
years = 1:length(nanmeans); % Assuming each script corresponds to one year
degree = 2; % Set the degree of the polynomial curve fitting
coefficients = polyfit(years, nanmeans, degree);
curve = polyval(coefficients, years);
plot(years, nanmeans, 'o', years, curve);
xlabel('Year');
ylabel('nanmean');
title('Polynomial Curve Fitting');
legend('Data Points', 'Curve Fitting');
  2 Comments
Stephen23
Stephen23 on 20 Jun 2023
Do not use the variable name NANMEAN, because this shadows the inbuilt function of the same name.

Sign in to comment.


Sarthak
Sarthak on 19 Jun 2023
Hello Jincy,
What I understand from your question is that you want to plot the data from separate MATLAB files on a single plot. For that, you can add the required MATLAB files to the MATLAB Search Path and then use them to plot the curve. You can add a folder containing the files to the MATLAB path by:
  1. Right click on the folder.
  2. Click on ‘Add to Path’.
You can also use the ‘addpath’ function in MATLAB to add your files/folders to the MATLAB search path, the link of whose documentation is attached below:
Once you add the files to the path, you can simply load the files where you want to plot them by using load(‘fileName.m’).
Hope this helps!!

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!