Assign values to multiple subplots for secondary y axis

3 Ansichten (letzte 30 Tage)
Frances Synnott
Frances Synnott am 12 Nov. 2021
Kommentiert: Raj Bhakta am 25 Nov. 2021
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
Seth Furman am 15 Nov. 2021
Can you attach an example of your data?
Am I correct in understanding the following?
  • 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.
Frances Synnott
Frances Synnott am 18 Nov. 2021
Hi Seth, this is an example of the data I would like to plot. At the moment, I have 100 files each with their own x and y data that I would like to plot like the figure below. At the moment, I have been able to plot it like this by off setting the y axis for each subsequent plot to get this waterfall effect. However, I have another csv file that says file one = 20N, file two = 40N etc etc like the second y-axis in the image. So what I am looking to do is to write code that says file one = column one,cell one in the spreadsheet, file two = column one, cell two etc etc so I can plot a waterfall plot with two axes. I hope this makes sense! Thanks.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Raj Bhakta
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
Frances Synnott
Frances Synnott am 21 Nov. 2021
Hi Raj, I'm looking to plot each file on the sample plot, just like a waterfall plot. As the loads I am looking at don't increase with equal increments, such as 20N, 40N...85N for example, I need to find a way that assigns the first plot to the first cell of another csv and so on. Thank you for your help though.
Raj Bhakta
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?

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by