来自python的Matlab脚本调用不起作用

时间:2017-08-06 18:41:58

标签: python matlab python-3.x

所以我试图运行我从python编写的matlab脚本。当我在matlab界面中运行脚本时,它运行正常,但是当我从python运行它时它不起作用。基本上,脚本生成数据,绘制数据并保存图形。当我从python中调用它时,它会绘制它,保存图形,但每当我打开图形时它只是空白。从python运行时没有生成错误。每当我从python运行它时,数字都不会保持打开状态。它的窗口停留一秒然后它就消失了。

对于python:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.bruteForce(nargout=0)

我的matlab脚本名为bruteForce.m

close all
clear
dataIgnore = 1;  % Variable so I can skip some parts of the data if I want
timeData = csvread('NeuronNetworkClass/CA3/Time/time1.txt');
dataLength = length(timeData);
timeData = timeData(dataIgnore:dataLength);
totalTimeinMs = timeData(length(timeData));


%% Phase One: Gathering all the data which is in the form of csv files.

% Gather Inhibitory Neuron Data
ca3_numberOfInhibitoryNeuronFiles =     length(dir('NeuronNetworkClass/CA3/InhibitoryNeuron/Voltage'))-2;

ca3_inhibitoryNeuronVoltageData = cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/Voltage/voltage%d.txt', k);
    ca3_inhibitoryNeuronVoltageData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronVoltageData{k} = ca3_inhibitoryNeuronVoltageData{k}    (dataIgnore:dataLength);
end

ca3_inhibitoryNeuronSpikeTimeData = cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/SpikeTime/spikeTime%d.txt', k);
    ca3_inhibitoryNeuronSpikeTimeData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronSpikeTimeData{k} =     ca3_inhibitoryNeuronSpikeTimeData{k}    (ca3_inhibitoryNeuronSpikeTimeData{k}>timeData(1));
end

ca3_inhibitoryNeuronInhibitoryCurrentData =     cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/InhibitoryCurrent/inhibCurrent%d.    txt', k);
    ca3_inhibitoryNeuronInhibitoryCurrentData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronInhibitoryCurrentData{k}=     ca3_inhibitoryNeuronInhibitoryCurrentData{k}(dataIgnore:dataLength);
end

ca3_inhibitoryNeuronExcitatoryCurrentData =     cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/ExcitatoryCurrent/exciteCurrent%d    .txt', k);
    ca3_inhibitoryNeuronExcitatoryCurrentData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronExcitatoryCurrentData{k}=ca3_inhibitoryNeuronExcitatoryCurrent    Data{k}(dataIgnore:dataLength);
end

ca3_inhibitoryNeuronBackgroundCurrentData =     cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/BackgroundCurrent/bg%d.txt', k);
    ca3_inhibitoryNeuronBackgroundCurrentData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronBackgroundCurrentData{k}=ca3_inhibitoryNeuronBackgroundCurrent    Data{k}(dataIgnore:dataLength);
end

ca3_inhibitoryNeuronNMDACurrentData =     cell(0,ca3_numberOfInhibitoryNeuronFiles);
for k = 1: ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/InhibitoryNeuron/NMDA/nmda%d.txt', k);
    ca3_inhibitoryNeuronNMDACurrentData{k} = csvread(myfilename);
    ca3_inhibitoryNeuronNMDACurrentData{k}=ca3_inhibitoryNeuronNMDACurrentData{k}    (dataIgnore:dataLength);
end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Gather Excitatory Neuron Data
ca3_numberOfExcitatoryNeuronFiles =         length(dir('NeuronNetworkClass/CA3/ExcitatoryNeuron/Voltage'))-2;

ca3_excitatoryNeuronVoltageData = cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/Voltage/voltage%d.txt', k);
    ca3_excitatoryNeuronVoltageData{k - ca3_numberOfInhibitoryNeuronFiles} =     csvread(myfilename);
    ca3_excitatoryNeuronVoltageData{k -     ca3_numberOfInhibitoryNeuronFiles}=ca3_excitatoryNeuronVoltageData{k - ca3_numberOfInhibitoryNeuronFiles}(dataIgnore:dataLength);
end

ca3_excitatoryNeuronSpikeTimeData = cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/SpikeTime/spikeTime%d.txt', k);
    ca3_excitatoryNeuronSpikeTimeData{k - ca3_numberOfInhibitoryNeuronFiles} =     csvread(myfilename);
    ca3_excitatoryNeuronSpikeTimeData{k - ca3_numberOfInhibitoryNeuronFiles} =     ca3_excitatoryNeuronSpikeTimeData{k - ca3_numberOfInhibitoryNeuronFiles}    (ca3_excitatoryNeuronSpikeTimeData{k -    ca3_numberOfInhibitoryNeuronFiles}>timeData(1));
end

ca3_excitatoryNeuronInhibitoryCurrentData =     cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/InhibitoryCurrent/inhibCurrent%d.    txt', k);
    ca3_excitatoryNeuronInhibitoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles} = csvread(myfilename);
    ca3_excitatoryNeuronInhibitoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles} = ca3_excitatoryNeuronInhibitoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}(dataIgnore:dataLength);
end

ca3_excitatoryNeuronExcitatoryCurrentData =     cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/ExcitatoryCurrent/exciteCurrent%d    .txt', k);
    ca3_excitatoryNeuronExcitatoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles} = csvread(myfilename);
    ca3_excitatoryNeuronExcitatoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}=ca3_excitatoryNeuronExcitatoryCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}(dataIgnore:dataLength);
end

ca3_excitatoryNeuronBackgroundCurrentData =     cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/BackgroundCurrent/bg%d.txt', k);
    ca3_excitatoryNeuronBackgroundCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles} = csvread(myfilename);
    ca3_excitatoryNeuronBackgroundCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}=ca3_excitatoryNeuronBackgroundCurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}(dataIgnore:dataLength);
end

ca3_excitatoryNeuronNMDACurrentData =     cell(0,ca3_numberOfExcitatoryNeuronFiles);
for k = 1 + ca3_numberOfInhibitoryNeuronFiles:     ca3_numberOfExcitatoryNeuronFiles + ca3_numberOfInhibitoryNeuronFiles
    myfilename =     sprintf('NeuronNetworkClass/CA3/ExcitatoryNeuron/NMDA/nmda%d.txt', k);
    ca3_excitatoryNeuronNMDACurrentData{k - ca3_numberOfInhibitoryNeuronFiles} = csvread(myfilename);
    ca3_excitatoryNeuronNMDACurrentData{k -     ca3_numberOfInhibitoryNeuronFiles}=ca3_excitatoryNeuronNMDACurrentData{k - ca3_numberOfInhibitoryNeuronFiles}(dataIgnore:dataLength);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Phase 2: Calculating total field potentials

% Lists containing averages
ca3_PNFieldPotential = zeros(1,length(timeData));
ca3_BasketFieldPotential = zeros(1,length(timeData));
ca3_TotalFieldPotential = zeros(1,length(timeData));

% Calculating the total average, basket cell avg, and pyramidal cell avg
for z = 1: length(timeData)
    pnNeuronVoltageOfASingleDt = zeros(1,length(ca3_excitatoryNeuronVoltageData));
    for k = 1: length(ca3_excitatoryNeuronVoltageData)
        pnNeuronVoltageOfASingleDt(k) = ca3_excitatoryNeuronVoltageData{k}(z);
    end

    bCellVoltageOfASingleDt = zeros(1,length(ca3_inhibitoryNeuronVoltageData));
    for l = 1: length(ca3_inhibitoryNeuronVoltageData)
        bCellVoltageOfASingleDt(l) = ca3_inhibitoryNeuronVoltageData{l}(z);
    end

    ca3_PNFieldPotential(z) = mean(pnNeuronVoltageOfASingleDt);
    ca3_BasketFieldPotential(z) = mean(bCellVoltageOfASingleDt);

    allNeuronVoltageOfASingleDt = [pnNeuronVoltageOfASingleDt bCellVoltageOfASingleDt];
    ca3_TotalFieldPotential(z) = mean(allNeuronVoltageOfASingleDt);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Phase 3: Calculating the fourrier transform of the total field potential

Fs = 1000/(timeData(2)-timeData(1));   % Sampling frequency                    
T  = 1/Fs;                             % Sampling period       
L  = length(ca3_TotalFieldPotential);  % Length of signal
t  = (0:L-1)*T;                        % Time vector

X = ca3_TotalFieldPotential - mean(ca3_TotalFieldPotential); % x = x-mean(x)
ca3_fourrierTransformOfFieldPotential = fft(X);

ca3_P2 = abs(ca3_fourrierTransformOfFieldPotential/L);
ca3_P1 = ca3_P2(1:L/2+1);
ca3_P1(2:end-1) = 2*ca3_P1(2:end-1);
ca3_frequencyVectorForTransform = Fs*(0:(L/2))/L;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Phase 4: Plotting the data

a = figure();
movegui(a,'north');
hold on
title('Total Field Potential')
xlabel('Time (ms)')
ylabel('Volts (V)')
plot(timeData,ca3_TotalFieldPotential,'k')
legend('CA3')
hold off


b = figure();
movegui(b,[0,50]);
hold on
title('Basket Cell Field Potential')
xlabel('Time (ms)')
ylabel('Volts (V)')
plot(timeData,ca3_BasketFieldPotential,'r')
legend('CA3')
hold off

c = figure();
movegui(c,[720,50]);
hold on
title('Pyramidal Cell Field Potential')
xlabel('Time (ms)')
ylabel('Volts (V)')
plot(timeData,ca3_PNFieldPotential)
legend('CA3')
hold off


d = figure();
hold on
movegui(d,[358,50]);
xlim([0 200])
title('Single-Sided Amplitude Spectrum of Total Field Potential for CA3')
xlabel('f (Hz)')
ylabel('|P1(f)|')
plot(ca3_frequencyVectorForTransform,ca3_P1)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Phase 5: saving the fourrier transform  
saveas(d,'/Users/Abid/Documents/MATLAB/NeuronNetworkClass/BRUTE_FORCE_DATA/fourrier.fig');

0 个答案:

没有答案