Help solve an error in displaying data
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
So to preface this, I am pretty new with MatLab and got some help from an AI friend. I have a script that loads a .csv file and creates a plot showcasing the intesity of a transcription start site (TSS) over the course of 24 hours. Now since I don't want to do that manually for 250 cells, I tried to automate the process so once I start my script, I get figures for all my files without having to manually import data for each new cell.
I got the script running but I can't seem to figure out how to make the script find the right TrackID (Which should be column 2 in the csv) and the Intensity (which should be column 12). Can anyone figure out why I still get empty figures when running the script?
Attached I have included an example-csv file.
Thank you.
% Specify the folder containing your files
folderPath = 'insert file path here';
saveFolderPath = 'insert desired save path here';
% Get a list of all files in the folder
files = dir(fullfile(folderPath, 'export*.csv')); % Assuming filenames start with 'export' and have a '.csv' extension
for l = 1:length(files)
% Get the current file name
currentFile = files(l).name;
% Extract values of z and w from the filename
[~, fileName, ~] = fileparts(currentFile);
parts = strsplit(fileName, '_');
z = str2double(parts{3}(3:end));
w = str2double(parts{4}(2:end));
% Load data from the current file
A = readtable(fullfile(folderPath, currentFile));
%import table values on matlab
Str=importdata(currentFile);
A=Str.data(:,:);
%i=line in the table
% Determine the column indices for TrackID and Intensity based on the data structure
trackIDColumn = 2; % Adjust this based on the actual structure of your data
intensityColumn = 12; % Adjust this based on the actual structure of your data
% Initialize variables
j = 0;
x0 = [];
k = 0;
x1 = [];
notvalid = 0;
sz = size(A);
% Loop through the data
for i = 1:sz(1)
if A(i, trackIDColumn) == z
j = j + 1;
x0(j, 1) = A(i, 7); % time 0
x0(j, 2) = A(i, intensityColumn); % intensity 0
elseif A(i, trackIDColumn) == w
k = k + 1;
x1(k, 1) = A(i, 7); % time 1
x1(k, 2) = A(i, intensityColumn); % intensity 1
else
notvalid = notvalid + 1;
end
end
% Rest of your existing script...
MS2 = zeros(481, 2);
for TrackID = 0:200
temp = A(A(:, trackIDColumn) == TrackID, [7, intensityColumn]);
MS2(temp(:, 1) + 1, TrackID + 1) = temp(:, 2);
end
% Check if w+1 is a valid index for MS2
if w+1 >= 1 && w + 1 <= size(MS2, 2)
MS2_mirror = -MS2(:, w + 1); % Adjusted for 'w'
MS2_mirror = -MS2(:, w + 1); % Adjusted for 'w'
timeframe = (1:481)';
time = timeframe * 3 - 3;
% TSS1=MS2(:,z),
% Plot
plot(time, MS2(:, z + 1), 'DisplayName', ['TSS 1']) % Adjusted for 'z'
hold on
plot(time, MS2_mirror, 'DisplayName', ['TSS 2']) % Adjusted for 'w'
xlabel('Time (min)', 'FontSize', 10)
xlim([0 1440])
ylabel('Fluorescent intensity', 'FontSize', 10)
legend(['TSS 1'], ['TSS 2'])
else
disp(['Invalid TrackID for w: ' num2str(w)]);
end
% Optionally, you can include a pause or any other necessary delay here
pause(0.5); % Adjust as needed
% Save the figure as JPEG
figJPEGName = fullfile(saveFolderPath, ['Figure_' num2str(z) '_and_' num2str(w) '_' fileName '.jpg']);
saveas(gcf, figJPEGName, 'jpeg');
% Save the figure as MATLAB Figure file
figMatName = fullfile(saveFolderPath, ['Figure_' num2str(z) '_and_' num2str(w) '_' fileName '.fig']);
saveas(gcf, figMatName, 'fig');
% Optionally, you can include a pause or any other necessary delay here
pause(0.5); % Adjust as needed
% End of the loop
close(gcf); % Close the current figure to avoid overlapping in the next iteration
end
% Additional cleanup or finalization code can be added here
disp('Processing complete.');
0 Kommentare
Akzeptierte Antwort
Voss
am 19 Jan. 2024
Observation 1: You have
currentFile = files(l).name;
so currentFile is the name of the file only, without any absolute or relative path, so when you do this:
Str=importdata(currentFile);
you are asking to importdata a file in the current directory that may or may not exist.
Instead, to be sure you are referring to the correct file, you can use:
currentFile = fullfile(files(l).folder,files(l).name);
That doesn't sound like the problem you ran into, because you say the code does generate fig files but the figures are empty. (However, if you make the same mistake as above when checking for the fig files, you may not be looking at the files that were generated in the most recent run of the code, if any were generated at all.)
Observation 2: z and w are both NaN with the example file
z = str2double(parts{3}(3:end));
w = str2double(parts{4}(2:end));
The code doesn't seem to conform to the file naming convention, at least for this example file.
If you change it to:
z = str2double(parts{3});
w = str2double(parts{4});
Then you get 0 and 1 for z and w, respectively. Is that what is intended? (Or should they have been NaN? If you want to allow NaNs for z and w, you should use isnan(A(i, trackIDColumn)) instead of A(i, trackIDColumn)==z, etc., because NaN==NaN is false.)
Making those two changes (and removing the redundant readtable call whose result is immediately overwritten) produces a fig file containing a non-empty figure:
% Specify the folder containing your files
folderPath = '.';
saveFolderPath = '.';
% Get a list of all files in the folder
files = dir(fullfile(folderPath, 'export*.csv')); % Assuming filenames start with 'export' and have a '.csv' extension
for l = 1:length(files)
% Get the current file name
% currentFile = files(l).name;
currentFile = fullfile(files(l).folder,files(l).name);
% Extract values of z and w from the filename
[~, fileName, ~] = fileparts(currentFile);
parts = strsplit(fileName, '_');
% z = str2double(parts{3}(3:end));
% w = str2double(parts{4}(2:end));
z = str2double(parts{3});
w = str2double(parts{4});
% % Load data from the current file
% A = readtable(currentFile);
%import table values on matlab
Str=importdata(currentFile);
A=Str.data(:,:);
%i=line in the table
% Determine the column indices for TrackID and Intensity based on the data structure
trackIDColumn = 2; % Adjust this based on the actual structure of your data
intensityColumn = 12; % Adjust this based on the actual structure of your data
% Initialize variables
j = 0;
x0 = [];
k = 0;
x1 = [];
notvalid = 0;
sz = size(A);
% Loop through the data
for i = 1:sz(1)
if A(i, trackIDColumn) == z
j = j + 1;
x0(j, 1) = A(i, 7); % time 0
x0(j, 2) = A(i, intensityColumn); % intensity 0
elseif A(i, trackIDColumn) == w
k = k + 1;
x1(k, 1) = A(i, 7); % time 1
x1(k, 2) = A(i, intensityColumn); % intensity 1
else
notvalid = notvalid + 1;
end
end
% Rest of your existing script...
MS2 = zeros(481, 2);
for TrackID = 0:200
temp = A(A(:, trackIDColumn) == TrackID, [7, intensityColumn]);
MS2(temp(:, 1) + 1, TrackID + 1) = temp(:, 2);
end
% Check if w+1 is a valid index for MS2
if w+1 >= 1 && w + 1 <= size(MS2, 2)
MS2_mirror = -MS2(:, w + 1); % Adjusted for 'w'
MS2_mirror = -MS2(:, w + 1); % Adjusted for 'w'
timeframe = (1:481)';
time = timeframe * 3 - 3;
% TSS1=MS2(:,z),
% Plot
plot(time, MS2(:, z + 1), 'DisplayName', ['TSS 1']) % Adjusted for 'z'
hold on
plot(time, MS2_mirror, 'DisplayName', ['TSS 2']) % Adjusted for 'w'
xlabel('Time (min)', 'FontSize', 10)
xlim([0 1440])
ylabel('Fluorescent intensity', 'FontSize', 10)
legend(['TSS 1'], ['TSS 2'])
else
disp(['Invalid TrackID for w: ' num2str(w)]);
end
% Optionally, you can include a pause or any other necessary delay here
pause(0.5); % Adjust as needed
% Save the figure as JPEG
figJPEGName = fullfile(saveFolderPath, ['Figure_' num2str(z) '_and_' num2str(w) '_' fileName '.jpg']);
saveas(gcf, figJPEGName, 'jpeg');
% Save the figure as MATLAB Figure file
figMatName = fullfile(saveFolderPath, ['Figure_' num2str(z) '_and_' num2str(w) '_' fileName '.fig']);
saveas(gcf, figMatName, 'fig');
% Optionally, you can include a pause or any other necessary delay here
pause(0.5); % Adjust as needed
% End of the loop
close(gcf); % Close the current figure to avoid overlapping in the next iteration
end
% Additional cleanup or finalization code can be added here
disp('Processing complete.');
% open the fig files to check:
fig_files = dir(fullfile(saveFolderPath,'*.fig'));
for ii = 1:numel(fig_files)
openfig(fullfile(fig_files(ii).folder,fig_files(ii).name))
end
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu External Language Interfaces 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!
