- Each file has one set of x and y-values.
- You'd like to plot each set of x and y-values on a separate subplot.
Assign values to multiple subplots for secondary y axis
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a graph that is plotting 100 different subplots. Each subplot has an x axis (degrees) and y axis (intensity), and also refers to a different load during tensile testing. For example, file one is 20N, file two is 30N, etc etc. I have my subplot working perfectly but I am trying to read the load values from a xls file and want to assign the first value in that spreadsheet to the first subplot, and so on, and to use this as a second y-axis. This is my code so far but I can't figure out how to assign the load to each file. (I have excluded my filepaths from the directories for this question just in case someone points that out.) I would really appreciate it if someone could help!
close all
clear all
clc %Closes opened figures
%D is directory - where the files are saved.
D = '.......';
S = dir(fullfile(D,'*txt')); % only looking at .txt files
N = numel(S); %N = number of .txt files
C = cell(1,N);
F1 = fullfile('........'); %reading files
C1 = xlsread(F1,'A2:A108');
M = numel(C1);
for k = 1:N %reads every second file. If you want to read all files, use "for k = 1:N" instead.
F = fullfile(D,S(k).name); %reading files
T = importdata(F,' ',20); %converting files into a table
C{k} = T.data;
F1 = fullfile('......'); %reading files
x = (C{k}(:,1:1)); %2 theta
y = (C{k}(:,2:2)); %Intensity%
plot(x,y,'b');
set(gca,'YTickLabel',[]); %removes y axis values
title('Diffraction Waterfall Plot')
xlabel('2 theta');
ylabel('Intensity');
plot(x,y)
hold on
end
2 Kommentare
Seth Furman
am 15 Nov. 2021
Can you attach an example of your data?
Am I correct in understanding the following?
Antworten (1)
Raj Bhakta
am 21 Nov. 2021
Frances,
Are you attempting to keep each dataset (20N, 40N, ...) on it's own separate subplot? If so, you can try something like this:
figure;
subplot(3,1,1)
hAx = plot(1:10, rand(10,1));
yyaxis left; ylabel('Left Side 1'); set(gca,'YTick',[]);
yyaxis right; ylabel('Right Side 1')
subplot(3,1,2)
plot(1:10, rand(10,1))
yyaxis left; ylabel('Left Side 2'); set(gca,'YTick',[]);
yyaxis right; ylabel('Right Side 2')
subplot(3,1,3)
plot(1:10, rand(10,1))
yyaxis left; ylabel('Left Side 3'); set(gca,'YTick',[]);
yyaxis right; ylabel('Right Side 3')
I hid the left y axis because you're using it more as a title (20N,40N,...) instead of as a scale. Is this what you're looking for?
2 Kommentare
Raj Bhakta
am 25 Nov. 2021
Frances,
I think I understand. Is the load condition recorded somewhere in your txt file that you're reading in or in the name of the file? You can convert that string to a number and use this as your offset to move up the waterfall by your load increment.
loadOffset = 0; % The initial offset on the 2nd y axis is 0
for k = 1:N %reads every second file. If you want to read all files, use "for k = 1:N" instead.
F = fullfile(D,S(k).name); %reading files
T = importdata(F,' ',20); %converting files into a table
C{k} = T.data;
newLoading = str2num(C{k}(row,col)); % Get the loading offset
loadOffset = loadOffset + newLoading;
x = (C{k}(:,1:1)); %2 theta
y = (C{k}(:,2:2)); %Intensity%
y = y + loadOffset; % Add the load offset to all y values
plot(x,y,'b');
set(gca,'YTickLabel',[]); %removes y axis values
title('Diffraction Waterfall Plot')
xlabel('2 theta');
ylabel('Intensity');
plot(x,y)
hold on
end
Would this work?
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!