필터 지우기
필터 지우기

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

조회 수: 56 (최근 30일)
Jincy
Jincy 2023년 6월 19일
댓글: Jincy 2023년 6월 20일
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
Stephen23
Stephen23 2023년 6월 20일
편집: Stephen23 2023년 6월 20일
"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 2023년 6월 20일
I am just save the scripts of 21 years seperately in a new folder to find the nanmean of indian using shapefile

댓글을 달려면 로그인하십시오.

답변 (2개)

Diwakar Diwakar
Diwakar Diwakar 2023년 6월 19일
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
Jincy
Jincy 2023년 6월 20일

It showing this error

Stephen23
Stephen23 2023년 6월 20일
Do not use the variable name NANMEAN, because this shadows the inbuilt function of the same name.

댓글을 달려면 로그인하십시오.


Sarthak
Sarthak 2023년 6월 19일
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!!
  댓글 수: 1
Jincy
Jincy 2023년 6월 20일
Path is correctly displayed..but i couldn't read all the .m files

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Preprocessing Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by