Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java方法将数据从Matlab发布到Pachube(Cosm)_Java_Matlab_Post_Cosm - Fatal编程技术网

使用Java方法将数据从Matlab发布到Pachube(Cosm)

使用Java方法将数据从Matlab发布到Pachube(Cosm),java,matlab,post,cosm,Java,Matlab,Post,Cosm,我正在使用和Matlab将数据发送到我的数据流。以下java代码适用于我的机器: package smartclassroom; import Pachube.Data; import Pachube.Feed; //import Pachube.FeedFactory; import Pachube.Pachube; import Pachube.PachubeException; public class SendFeed { public static void ma

我正在使用和Matlab将数据发送到我的数据流。以下java代码适用于我的机器:

package smartclassroom;
import Pachube.Data;
import Pachube.Feed;
//import Pachube.FeedFactory;
import Pachube.Pachube;
import Pachube.PachubeException;

public class SendFeed {     

    public static void main(String arsg[]) throws InterruptedException{
        SendFeed s = new SendFeed(0.0);
        s.setZainteresovanost(0.3);
        double output = s.getZainteresovanost();      
        System.out.println("zainteresovanost " + output);             

        try {
                Pachube p = new Pachube("MYAPIKEY");
                Feed f = p.getFeed(MYFEED);
                f.updateDatastream(0, output);
            } catch (PachubeException e) {
                System.out.println(e.errorMessage);
            }
    }

    private double zainteresovanost;    
    public SendFeed(double vrednost) {
        zainteresovanost = vrednost;
    }
    public void setZainteresovanost(double vrednost) {
        zainteresovanost = vrednost;
    }
    public double getZainteresovanost() {
        return zainteresovanost;
    }

}
但我需要从Matlab中完成这项工作。我尝试过重写(link中的示例正在我的机器上工作):我用javac编译java类,并将JPachube.jar和SendFeed.class添加到path中,然后在Matlab中使用以下代码:

javaaddpath('C:\work')
javaMethod('main','SendFeed','');    
pachubeValue = SendFeed(0.42);
 javaaddpath('c:\work\JPachube.jar')

import Pachube.Data.*
import Pachube.Feed.*
import Pachube.Pachube.*
import Pachube.PachubeException.*

pachube = Pachube.Pachube('MYAPIKEY');
feed = pachube.getFeed(MYFEED);
feed.updateDatastream(0, 0.54);
我得到一个错误:

??? Error using ==> javaMethod
No class SendFeed can be located on Java class path

Error in ==> post_to_pachube2 at 6
javaMethod('main','SendFeed','');
这很奇怪,因为正如我所说,链接中的示例正在工作

之后,我决定将JPachube直接包含在Matlab代码中,并在Matlab中编写等效代码:

javaaddpath('C:\work')
javaMethod('main','SendFeed','');    
pachubeValue = SendFeed(0.42);
 javaaddpath('c:\work\JPachube.jar')

import Pachube.Data.*
import Pachube.Feed.*
import Pachube.Pachube.*
import Pachube.PachubeException.*

pachube = Pachube.Pachube('MYAPIKEY');
feed = pachube.getFeed(MYFEED);
feed.updateDatastream(0, 0.54);
我得到了这个错误:

??? No method 'updateDatastream' with matching signature found for class 'Pachube.Feed'.

Error in ==> post_to_pachube2 at 12
feed.updateDatastream(0, 0.54);

所以我几乎什么都试过了,什么都没试过!任何使这项工作的方法对我来说都是好的。提前谢谢你的帮助

我宁愿使用内置方法。Matlab有/,如果您只想从Cosm API请求一些CSV数据,它就可以工作。如果您确实需要使用JSON,可以在中处理它

通过Cosm API密钥,可以通过
key
参数完成,如下所示:

cosm_feed_url = "https://api.cosm.com/v2/feeds/61916.csv?key=<API_KEY>"
cosm_feed_csv = urlread(cosm_feed_url)
cosm\u feed\u url=”https://api.cosm.com/v2/feeds/61916.csv?key=. 事实上,
urlwrite
函数只是为文件输入而设计的,我甚至看不到任何关于如何使用格式化字符串的官方示例。创建一个临时文件是合理的,除非只有几行CSV。 你可能需要用它来做更严重的事情


更新:似乎
urlread2
我更愿意使用内置方法。Matlab有/,如果您只想从Cosm API请求一些CSV数据,它可以工作。如果您确实需要使用JSON,可以在中处理

通过Cosm API密钥,可以通过
key
参数完成,如下所示:

cosm_feed_url = "https://api.cosm.com/v2/feeds/61916.csv?key=<API_KEY>"
cosm_feed_csv = urlread(cosm_feed_url)
cosm\u feed\u url=”https://api.cosm.com/v2/feeds/61916.csv?key=. 事实上,
urlwrite
函数只是为文件输入而设计的,我甚至看不到任何关于如何使用格式化字符串的正式示例。创建一个临时文件是合理的,除非只有几行CSV。 你可能需要用它来做更严重的事情

更新:似乎
urlread2

这个技巧已经为我完成了(来自的回答)

这对我来说是个骗局(回答来自)


我希望我能扩展这个例子,但不幸的是我手头没有Matlab。我发现urlread默认使用硬编码的内容类型。我需要使用urlead发送帖子,内容类型为text/plain;charset=utf-8,并将api键添加到标头。我希望可以扩展示例,但不幸的是,我手头没有Matlab。我发现urlread默认使用硬编码的内容类型。我需要使用urlead发送帖子,内容类型为text/plain;charset=utf-8并将api键添加到标头。这当然是一个好的解决方案,尽管您需要获取JAR,而
urlread2
使用已经可用的
sun.net.www.protocol.http
sun.net.www.protocol.https
。我想,如果您要使用的只是一个API端点,那么您可以从
urlread2
中提取一些代码位,并将其放入函数中,从而使您的代码几乎没有依赖性。我不太确定Apache HTTP客户端是否比Sun的好,有什么想法吗?这个解决方案很简单。我曾多次尝试使用urlread2发布具有不同标题的数据,但都没有成功。无论如何,谢谢:)这当然是一个很好的解决方案,尽管您需要获取jar,而
urlread2
使用已经可用的
sun.net.www.protocol.http
sun.net.www.protocol.https
。我想,如果您要使用的只是一个API端点,那么您可以从
urlread2
中提取一些代码位,并将其放入函数中,从而使您的代码几乎没有依赖性。我不太确定Apache HTTP客户端是否比Sun的好,有什么想法吗?这个解决方案很简单。我曾多次尝试使用urlread2发布具有不同标题的数据,但都没有成功。无论如何,谢谢你:)